在C++中怎么对vector<string>类型容器中的元素进行遍历?
#include<iostream>#include<string>#include<vector>usingnamespacestd...
#include<iostream>#include<string>#include<vector>using namespace std;int main(int argc, char** argv){ string line; vector<string> v; string word; int i, j = 0; cout << "Enter a line:"; getline(cin, line); for(i = 0; i <= line.size(); i++) { if(line[i] != ' ' && line[i] != '\0') { word[j] = line[i]; j++; } else { bool exist = false; //cout << "run" << endl; word[j] = '\0'; for(vector<string>::iterator it = v.begin(); it != v.end(); it++){ if(word == *it) exist = true; } if(exist == false) v.push_back(word); j = 0; } } cout << v.size();}
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询