C++中,vector参数为类型时,如何查找和添加元素?
比如,有一个structTable{intNUM(序号);stringCI(字符串);intcount(计数);};然后将Table放进vector,也就是vector<...
比如,有一个struct Table{int NUM(序号);string CI(字符串);int count(计数);};然后将Table放进vector,也就是vector<Table>,那么如何根据一个NUM序号,对对应字符串的计数加1操作?比如,我要对序号NUM为10的字符串计数加1,怎么查找,然后count加1?写程序。
展开
2个回答
展开全部
用stl的算法find_if,functor做谓词,感觉更简练一些
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Table {
Table(const int n, const string& s, const int c):NUM(n),CI(s),count(c){}
int NUM;
string CI;
int count;
};
class TheTable {
int NUM;
public:
TheTable(int i):NUM(i) {}
bool operator()(Table& t) {
return t.NUM == NUM;
}
};
int main () {
std::vector<Table> myvector;
Table t0(1, "hello", 2), t1(2, "world", 3);
myvector.push_back(t0);
myvector.push_back(t1);
for (int i=0; i<3; ++i) {
vector<Table>::iterator it = find_if(myvector.begin(), myvector.end(), TheTable(i));
if (it == myvector.end())
cout << "Not found" << endl;
else {
std::cout << "MyTable " << (*it).CI << '\n';
++(*it).count;
}
}
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Table {
Table(const int n, const string& s, const int c):NUM(n),CI(s),count(c){}
int NUM;
string CI;
int count;
};
class TheTable {
int NUM;
public:
TheTable(int i):NUM(i) {}
bool operator()(Table& t) {
return t.NUM == NUM;
}
};
int main () {
std::vector<Table> myvector;
Table t0(1, "hello", 2), t1(2, "world", 3);
myvector.push_back(t0);
myvector.push_back(t1);
for (int i=0; i<3; ++i) {
vector<Table>::iterator it = find_if(myvector.begin(), myvector.end(), TheTable(i));
if (it == myvector.end())
cout << "Not found" << endl;
else {
std::cout << "MyTable " << (*it).CI << '\n';
++(*it).count;
}
}
return 0;
}
2013-03-31
展开全部
晕,这个,看你的命名,我贴个程序给你吧,不知道是不是你要的,如果不是你再追问我,我给你讲。#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <string>
#include <iomanip>
using namespace std;struct Table
{
int Cid;
string CI;
size_t SC;
};int main()
{
vector<string> strlist;
vector<Table> tablist;
Table tmp;
ifstream file("a.bat");
string temp;
vector<string>::iterator iter;
vector<Table>::iterator i;
while(1)
{
file >> temp;
if(temp == "")
break;
strlist.push_back(temp);
}
tmp.Cid = 0;
tmp.CI = "0";
tmp.SC = 0;
tablist.push_back(tmp);
for(iter = strlist.begin(); iter != strlist.end(); iter++)
{
tmp.Cid = iter - strlist.begin() + 1;
tmp.CI = *iter;
tmp.SC = iter->size();
tablist.push_back(tmp);
}
cout << setiosflags(ios::left) << setw(10) << "Cid";
cout << setiosflags(ios::left) << setw(10) << "CI";
cout << setiosflags(ios::left) << setw(10) << "SC" << endl;
for(i = tablist.begin() + 1; i != tablist.end(); i++)
{
cout << setiosflags(ios::left) << setw(10) << i->Cid;
cout << setiosflags(ios::left) << setw(10) << i->CI;
cout << setiosflags(ios::left) << setw(10) << i->SC << endl;
}
return 0;
}
#include <fstream>
#include <vector>
#include <iterator>
#include <string>
#include <iomanip>
using namespace std;struct Table
{
int Cid;
string CI;
size_t SC;
};int main()
{
vector<string> strlist;
vector<Table> tablist;
Table tmp;
ifstream file("a.bat");
string temp;
vector<string>::iterator iter;
vector<Table>::iterator i;
while(1)
{
file >> temp;
if(temp == "")
break;
strlist.push_back(temp);
}
tmp.Cid = 0;
tmp.CI = "0";
tmp.SC = 0;
tablist.push_back(tmp);
for(iter = strlist.begin(); iter != strlist.end(); iter++)
{
tmp.Cid = iter - strlist.begin() + 1;
tmp.CI = *iter;
tmp.SC = iter->size();
tablist.push_back(tmp);
}
cout << setiosflags(ios::left) << setw(10) << "Cid";
cout << setiosflags(ios::left) << setw(10) << "CI";
cout << setiosflags(ios::left) << setw(10) << "SC" << endl;
for(i = tablist.begin() + 1; i != tablist.end(); i++)
{
cout << setiosflags(ios::left) << setw(10) << i->Cid;
cout << setiosflags(ios::left) << setw(10) << i->CI;
cout << setiosflags(ios::left) << setw(10) << i->SC << endl;
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询