vector <map<string,string>> dictionary;
map <string, string> word1;
map <string, string> word2;
word1.insert(pair<string, string>("UNREAL","Abc"));
word2.insert(pair<string, string>("PROPS","Efg"));
dictionary.push_back(word1);
dictionary.push_back(word2);
vector<map<string, string>>::iterator it;
it = dictionary.begin();
for( it; it != dictionary.end(); it++)
{
cout << it << " " << it << endl; //ERROR
}
我想显示存储在vector中的数据。请建议我如何显示矢量字典的输出?
答案 0 :(得分:5)
// i is each map in your vector
for (auto i : dictionary) {
// j is each std::pair<string,string> in each map
for (auto j : i) {
// these are the two strings in each pair
j.first; j.second;
}
}
这个答案需要c ++ 11,但是现在几乎所有东西都支持。
答案 1 :(得分:5)
为了解决您的问题,您应该这样做:
for(it = dictionary.begin(); it != dictionary.end(); it++){
for(auto it1=it->begin();it1!=it->end();++it1){
cout << it1->first << " " << it->second << endl;
}
}
但是,我觉得设计有问题。在您的情况下,您不需要vector
个map
...您需要vector
个pair
或map
。
对的矢量:
std::vector <std::pair<string,string>> dictionary;
dictionary.emplace_back("UNREAL","Abc");
dictionary.emplace_back("PROPS","Efg");
for(auto const& item:dictionary){
std::cout << item.first << " " << item.second;
}
<强>图:强>
std::map<string,string> dictionary;
dictionary.insert("UNREAL","Abc");//also : dictionary["UNREAL"]="Abc";
dictionary.insert("PROPS","Efg");//also : dictionary["PROPS"]="Efg";
for(auto const& item:dictionary){
std::cout << item.first << " " << item.second;
}
因为地图不仅仅是一对东西,它是一组对(有点不精确)。
答案 2 :(得分:1)
vector和map是两个容器,所以你需要两个嵌套循环。在地图上的嵌套循环中,您的迭代器引用std::pairs,因此您使用.first访问密钥,使用.second访问该值:
vector <map<string,string> > dictionary;
map <string, string> word1;
map <string, string> word2;
word1.insert(pair<string, string>("UNREAL","Abc"));
word2.insert(pair<string, string>("PROPS","Efg"));
dictionary.push_back(word1);
dictionary.push_back(word2);
vector<map<string, string> >::iterator it;
for( it = dictionary.begin(); it != dictionary.end(); ++it)
{
map<string, string>::iterator it;
for( nested = it.begin(); nested != it.end(); ++nested)
{
cout << it->first << " " << it->second << endl; //ERROR
}
}
如果您使用C ++ 11,则可以使用Range-based for-loops:
缩短时间vector <map<string,string>> dictionary;
map <string, string> word1;
map <string, string> word2;
word1.insert(pair<string, string>("UNREAL","Abc"));
word2.insert(pair<string, string>("PROPS","Efg"));
dictionary.push_back(word1);
dictionary.push_back(word2);
for(const map<string, string> &outer : dictionary)
{
for(const pair<string, string> & inner : outer)
{
cout << inner.first << " " << inner.second << endl; //ERROR
}
}
答案 3 :(得分:1)
对类似问题here有答案。该代码允许您使用Alamofire.request(urlString, method: .post, parameters: registrationModel.getParentCandidateDictionary(), encoding: JSONEncoding.default)
.validate()
.responseJSON() { response in
switch response.result {
case .failure:
// handle errors (including `validate` errors) here
if let statusCode = response.response?.statusCode {
if statusCode == 409 {
// handle 409 specific error here, if you want
}
}
case .success(let value):
// handle success here
print(value)
}
}
运算符使用任何模板参数打印任何 stl容器。