[英]Print size of a vector through an iterator
我正在嘗試打印vector
的大小 。 聽起來很簡單,但矢量在map
。 目前我在地圖上有一個迭代器,如下所示:
map<string, vector<map<vector<string> , vector<string> > > >::iterator it;
我試圖顯示這樣的大小:
編輯:
迭代器是這樣的初始化: it = csvMap.find(commandList.at(lineCount));
cout<<"Size of vector in Map after modifying: " << it->second.size() <<"\\n"<<endl;
它沒有用,程序崩潰了。 我認為一種方法是制作一個臨時矢量並用值it->second;
填充它it->second;
但只是為了獲得尺寸是一種浪費空間不是嗎?
有沒有更好的方法呢?
提前致謝!
EDIT2:刪除舊代碼
編輯3:新代碼:
map<vector<string> , vector<string> > parameterMap;
parameterMap.insert(pair<vector<string> , vector<string> > (
part1_input, part2_output));
map<string, vector<map<vector<string> , vector<string> > > >::iterator it;
cout<<"\nSize of CSV Map before modifying: " << csvMap.size() <<endl;
//cout<<"Size of vector in CSV Map before modifying: " << it->second.size() <<"\n"<<endl;
if(csvMap.size() == 0)
{
/*
* csvMap is empty -> no need to search for something. Just insert the fist entries
*/
listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
csvMap.insert(pair<string, vector<map<vector<string> ,
vector<string> > > > (commandList[lineCount],
listedParameterMap));
cout<<"CSV Map size: " << csvMap.size() <<endl;
}
else
{
/*
* Search if the Command is already available, if not,
* add it to the map with its corresponding list of maps (in/output values)
* find returns map::end if key is not found
*/
cout<<"Checking if: " << commandList.at(lineCount) << " is already in the list \n" << endl;
it = csvMap.find(commandList.at(lineCount));
if (it == csvMap.end())
{
/*
* it = csvMap.end() is true
* The command isn't found
*/
cout<< commandList.at(lineCount) << " command not available. Inserting it! \n" << endl;
listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
csvMap.insert(pair<string, vector<map<vector<string> ,
vector<string> > > > (commandList[lineCount],
listedParameterMap));
}
else
{
/*
* it != csvMap.end()
* The command is found. Append the parameterMap to the vector in the map
*/
cout<< commandList.at(lineCount) << " is already in the list! Appending parameters on pos: "<< it->second.size()-1<< "\n" << endl;
it->second.push_back(parameterMap);
}
}
cout<<"\nSize of CSV Map after modifying: " << csvMap.size() <<endl;
cout<<"Size of vector in CSV Map after modifying: " << it->second.size() <<"\n"<<endl;
我希望有人還在讀這個......
我現在發現it.second
似乎是第一次交互時的問題。 但我不明白為什么。
代碼段(也在上面的代碼中):
if(csvMap.size() == 0)
{
/*
* csvMap is empty -> no need to search for something. Just insert the fist entries
*/
listedParameterMap.insert(listedParameterMap.end(), 1, parameterMap);
csvMap.insert(pair<string, vector<map<vector<string> ,
vector<string> > > > (commandList[lineCount],
listedParameterMap));
cout<<"CSV Map size: " << csvMap.size() <<endl;
cout<<"listedParameterMap: " << listedParameterMap.size() <<endl;
cout<< commandList.at(lineCount) << " is already in the list! Appending parameters on pos: "<< it->second.size()<< "\n" << endl;
}
這似乎不起作用。 雖然它已經融入其中。 知道為什么嗎? 就我看來,comanndList和listedParameterMap就行了。
it = csvMap.find(commandList.at(lineCount));
if (it == csvMap.end()) {
cout << "not found\n";
}
else {
cout << "Size of vector in Map after modifying: " << it->second.size() << '\n';
}
無法找到命令或命令是最后一個
不,結束迭代器不是容器中的項。
string c = (*it).first;
由於這是在迭代器是結束迭代器之后,因此在解除引用它時會有未定義的行為。
it
指向一個無效的位置。 你需要使用map的迭代器來初始化它。 喜歡it = myMap.find("aaa"); //Replace it with actual key
東西it = myMap.find("aaa"); //Replace it with actual key
it = myMap.find("aaa"); //Replace it with actual key
在執行find
之后,你需要通過myMap.end()
檢查myMap.end()
來確保你有一個有效的迭代器。
編輯
你在這里使用未初始化的迭代器:
cout<<"Size of vector in CSV Map before modifying: " << it->second.size() <<"\n"<<endl;
此外,您csvMap.end()
引用指向csvMap.end()
的迭代器,它將導致再次崩潰。
根據編輯3
您仍在使用指向if(csvMap.size() == 0)
和if(it == csvMap.end())
情況的結束的unitialized迭代器/迭代器。 你需要使用insert
函數的返回值來初始化it
,如下所示: it = csvMap.insert(....).first;
在這些情況下。
除非刪除了該特定元素,否則映射迭代器不會失效。 這意味着你做了別的錯事。
你的收藏相當復雜。
為了檢查大小,你需要知道那里有一個元素,即find沒有返回你的地圖的“結束”。 如果是,則不能使用返回的迭代器。
當然你如何處理這個是你自己的決定,例如,如果找不到字符串,則返回-1(0表示已找到但沒有內容)。
現在您已經編輯了代碼我立即發現了一個錯誤:
if (it == csvMap.end())
{
/*
* it = csvMap.end() > true
* Either when command isn't found or command is the last
*/
string c = (*it).first;
你不能解除引用結束( it
是)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.