为了账号安全,请及时绑定邮箱和手机立即绑定

我的代码哪里有错?

#include <vector>

#include <map>

#include <string>

#include <iostream>

using namespace std;


int main(void)

{

    // 使用vector存储数字:3、4、8、4

    vector<int> vec;

    vec.push_back(3);

    vec.push_back(4);

    vec.push_back(8);

    vec.push_back(4);

    //循环打印数字

    

    vector<int>::iterator it=vec.begin();

    for(;it != vec.end();it++)

    {

        cout<<*it<<endl;

    }

    cout<<endl;

    

    // 使用map来存储字符串键值对

    map<string,string> m;

    pair<string,string> p1("a","hello");

    pair<string,string> p2("b","boy");

    pair<string,string> p3("c","cool");

    m.insert(p1);

    m.insert(p2);

    m.insert(p3);

   cout<<m.size();

  /* for(int i=0;i<m.size();i++)

   {

       cout<<m[i].first<<endl;

   }

   */

   map<string,string>::iterator it = m.begin();

   

    // 打印map中数据

    

   for(;it != m.end();it++)

    {

        cout<<it->first<<endl;

        cout<<it->second<<endl;

        cout<<endl;

    }

  

    return 0;

}


正在回答

2 回答

向量和容器中迭代器的命名重复了,都是it,得换掉。

0 回复 有任何疑惑可以回复我~

你的迭代器 it 重复定义了两次

后面的it改为it1就不会出错了

map<string, string>::iterator it1 = m.begin();

// 打印map中数据

for (; it1 != m.end(); it1++)

{

cout << it1->first << endl;

cout << it1->second << endl;

cout << endl;

}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之模板篇
  • 参与学习       91174    人
  • 解答问题       318    个

本C++教程力求即学即会,所有知识以实践方式讲解到操作层面

进入课程

我的代码哪里有错?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信