Richard_or_Ricardo 2016-08-07 15:10 采纳率: 0%
浏览 1099
已结题

无法用iterator遍历vector<char *>怎么办

#include
#include
using namespace std;
int main()
{
vector ivec;
char name[20];
cout << "请输入字符串" << endl;
while (cin >> name)
ivec.push_back(name);
for (vector::iterator it = ivec.begin(); it != ivec.end(); it ++)
cout << *it << endl;
cout << ivec.size() << endl;
cout << endl;
return 0;
}

运行结果为:
请输入字符串
ii jj kk
~Z

3

请按任意键继续......
更多 0 分享到:

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-08-07 16:07
    关注
     #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
    vector<char *> ivec;
    int i = 0;
    char name[100][20];
            cout << "请输入字符串" << endl;
    while (cin >> name[i])
    ivec.push_back(name[i++]);
    for (vector<char *>::iterator it = ivec.begin(); it != ivec.end(); it ++)
    cout << *it << endl;
    cout << ivec.size() << endl;
    cout << endl;
    return 0;
    }
    
    评论

报告相同问题?