写一个方法,找出数组中相同的数字和个数

yezilove7374521 2009-06-19 10:04:06
一个数组中找出相同数字和个数
...全文
127 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
use std::map<int, int> number-count
coolyama 2009-06-19
  • 打赏
  • 举报
回复

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

vector<int> int_vec;
vector<int> int_vec_backup;

void my_count() {
for ( vector<int>::const_iterator iter = int_vec.begin();iter != int_vec.end(); ++iter ) {
if ( int_vec_backup.end() == find( int_vec_backup.begin(), int_vec_backup.end(), *iter ) ) {
cout << *iter << "appears" << count( int_vec.begin(), int_vec.end(), *iter ) << "time(s)" << endl;
int_vec_backup.push_back( *iter );
}
else {
// nothing to do.
}
}

}
int main()
{
int n = 0;
cout << "please input next number :" << endl;
while ( cin >> n ) {
cout << "please input next number :" << endl;
int_vec.push_back( n );
}
my_count();
system( "pause" );
return 0;
}

一开始会提出输入数字,输入\n程序会停止要求输入数字并打印相同数字的个数。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧