亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

我的代碼哪里有錯?

#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++遠征之模板篇
  • 參與學習       91140    人
  • 解答問題       325    個

本C++教程力求即學即會,所有知識以實踐方式講解到操作層面

進入課程

我的代碼哪里有錯?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號