[英]c++ - iterating through a map of 3 elements
I'm very new to the use of STL containers in C++. 我对在C ++中使用STL容器非常陌生。
I have a map of 3 elements (2 strings as a pair - acting as the key, and an int acting as the value.) 我有一个由3个元素组成的映射(成对的2个字符串-充当键,而int充当值。)
map<pair<string, string>, int> wordpairs;
But when I try to iterate through it like this: 但是,当我尝试像这样遍历它时:
for (map<pair<string, string>, int> iterator i = wordpairs.begin(); i != wordpairs.end(); i++) {
cout << i->first << " " << i->second << "\n";
}
the compiler is throwing errors: 编译器抛出错误:
error: expected ‘;’ before ‘i’
for (map<pair<string, string>, int> iterator i = wordpairs.begin(); i != wordpairs.
^
error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
a7a.cpp:46:50: note: (if you use ‘-fpermissive’ G++ will accept your code)
error: cannot convert ‘std::map<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int> >}’ to ‘int’ in assignment
for (map<pair<string, string>, int> iterator i = wordpairs.begin(); i != wordpairs.
^
error: no match for ‘operator!=’ (operand types are ‘int’ and ‘std::map<std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >, int> >}’)
for (map<pair<string, string>, int> iterator i = wordpairs.begin(); i != wordpairs.
^
error: expected ‘)’ before ‘;’ token
pair<string, string>, int> iterator i = wordpairs.begin(); i != wordpairs.end(); i++) {
^
error: expected ‘;’ before ‘)’ token
pair<string, string>, int> iterator i = wordpairs.begin(); i != wordpairs.end(); i++) {
Not sure what I'm doing wrong here - this should be a simple fix though. 不知道我在这里做错了什么-虽然这应该是一个简单的解决方法。
You got the type wrong (you used spaces instead of ::
). 您输入的类型错误(使用空格而不是: ::
。
Map iterator gives you a key value pair -- and your key is a pair! 映射迭代器为您提供了一个键值对-您的键是一对! So you have a pair with a pair as a member. 因此,您有一对,其中一对是成员。 Here's an example that does roughly what you want to do. 这是一个大致可以完成您想做的例子。
#include <iostream> #include <map> #include <string> #include <utility> using namespace std; int main() { pair<string, string> my_key("To", "Be"); map<pair<string, string>, int> wordpairs { { {"Hello", "World"}, 33} }; for (const auto& kv : wordpairs) { cout << kv.first.first << ", " << kv.first.second << static_cast<char>(kv.second); } return 0; }
You forgot :: before iterator. 您在迭代器之前忘记了::。
You can also use the auto keyword: 您还可以使用auto关键字:
for (auto i = wordpairs.begin(); i != wordpairs.end(); ++i) {
cout << i->first << " " << i->second << "\n";
}
or simply using the range-based for loop: 或简单地使用基于范围的for循环:
for (auto& i : wordpairs) {
cout << i->first << " " << i->second << "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.