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;
    }
    
    评论
  • threenewbee 2016-08-07 16:08
    关注

    你那么写,根本只有一个字符串数组,所以输入了后面的,冲掉了前面的。

    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse无法正常运行
  • ¥15 定义了函数,但是无法根据函数定义触发器
  • ¥20 5变量卡诺图化简得出与非门电路图
  • ¥20 位置依赖的碱基序列独热编码
  • ¥15 Python爬取交通拥堵指数数据
  • ¥15 使用vba抓取重定向网页问题
  • ¥20 付费需求测试程序(细谈)。
  • ¥15 为什么这段c++代码会报这么多语法错误?
  • ¥20 如何利用C语言实现用最小二乘法选配两个经验公式
  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?