【发布时间】:2011-01-24 12:39:38
【问题描述】:
我正在尝试打印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;
它不工作,程序崩溃。
我认为一种方法是制作一个 temp vector 并用值 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 似乎是第一次交互的问题。但我不明白为什么。
代码sn-p(也在上面的代码中):
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;
}
这似乎不起作用。虽然它正在进入它。知道为什么吗? 据我所知,commanndList 和listedParameterMap 都可以。
【问题讨论】:
-
请发布迭代循环。我们需要上下文。
-
您在使用
it->second.size()之前检查if(it != csvMap.end())? -
您确定您正在搜索的
lineCount确实存在吗?搜索后是否检查与map::end的相等性? -
@Space_C0wb0y lineCount 有效。我检查了。 @Naveen 是的,如果里面什么都没有,我认为它只是 0...
-
我刚试过:即使我先拿出这个
cout<<...it->second.size() ...,我只是在它确定存在时尝试打印,它不起作用