如何访问向量的元素,其中向量映射为 unordered_map 中的字符 'key'?
How to access elements of a vector, where the vectors are mapped with a character 'key' in unordered_map?
我有一个 unordered_map,其中键是字符,值是向量:
unordered_map<char,vector<int> > table;
我正在尝试打印键及其值:
for(unordered_map<char,vector<int> > ::const_iterator iter = table.begin(); iter != table.end(); ++iter)
{
cout<<iter->first<< "\t";
for(vector<int> :: const_iterator iter1 = iter->second.begin(); iter1 != iter->second.end(); ++iter1)
{
cout<<(iter->second)[iter1];
}
cout<<"\n";
}
但是我遇到了这些我无法解决的错误:
15_09_2015.cpp: In function 'void printTable()':
15_09_2015.cpp:169:24: error: no match for 'operator[]' (operand types are 'const std::vector<int>' and 'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}')
cout<<(iter->second)[iter1];
^
15_09_2015.cpp:169:24: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32.8.1\include\c++\vector:64:0,
from 15_09_2015.cpp:3:
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:770:7: note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::reference = int&; std::vector<_Tp, _Alloc>::size_type = unsigned int]
operator[](size_type __n)
^
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:770:7: note: no known conversion for argument 1 from 'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}' to 'std::vector<int>::size_type {aka unsigned int}'
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:785:7: note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::const_reference = const int&; std::vector<_Tp, _Alloc>::size_type = unsigned int]
operator[](size_type __n) const
^
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:785:7: note: no known conversion for argument 1 from 'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}' to 'std::vector<int>::size_type {aka unsigned int}'
我做错了什么?如何打印 table?
operator[]
需要类型为 std::vector::size_type
的参数,因此 iterator
不能用作其参数。您可以将循环更改为:
for (vector<int>::size_type i = 0; i < iter->second.size(); i++)
{
cout<<(iter->second)[i];
}
或者您可以 cout<<*iter1;
打印迭代器指向的值。
for(vector<int> :: const_iterator iter1 = iter->second.begin(); iter1 != iter->second.end(); ++iter1)
{
cout<<*iter1;
}
要从迭代器中获取值,只需使用 *
取消引用它,就像您对指针所做的那样。但是在这里显式地使用迭代器是没有意义的。只需使用基于范围的 for 循环。
for(const auto& p : table)
{
cout << p.first << "\t";
for(const auto& i : p.second)
{
cout << i;
}
cout<<"\n";
}
您可以按以下方式更新代码以打印所有值,
for(auto iter = table.begin(); iter != table.end(); ++iter)
{
cout<<iter->first<< "\t";
for(auto iter1 = iter->second.begin(); iter1 != iter->second.end(); ++iter1)
{
cout<<*iter1;
}
cout<<"\n";
}
我有一个 unordered_map,其中键是字符,值是向量:
unordered_map<char,vector<int> > table;
我正在尝试打印键及其值:
for(unordered_map<char,vector<int> > ::const_iterator iter = table.begin(); iter != table.end(); ++iter)
{
cout<<iter->first<< "\t";
for(vector<int> :: const_iterator iter1 = iter->second.begin(); iter1 != iter->second.end(); ++iter1)
{
cout<<(iter->second)[iter1];
}
cout<<"\n";
}
但是我遇到了这些我无法解决的错误:
15_09_2015.cpp: In function 'void printTable()':
15_09_2015.cpp:169:24: error: no match for 'operator[]' (operand types are 'const std::vector<int>' and 'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}')
cout<<(iter->second)[iter1];
^
15_09_2015.cpp:169:24: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32.8.1\include\c++\vector:64:0,
from 15_09_2015.cpp:3:
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:770:7: note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::reference = int&; std::vector<_Tp, _Alloc>::size_type = unsigned int]
operator[](size_type __n)
^
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:770:7: note: no known conversion for argument 1 from 'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}' to 'std::vector<int>::size_type {aka unsigned int}'
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:785:7: note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::const_reference = const int&; std::vector<_Tp, _Alloc>::size_type = unsigned int]
operator[](size_type __n) const
^
c:\mingw\lib\gcc\mingw32.8.1\include\c++\bits\stl_vector.h:785:7: note: no known conversion for argument 1 from 'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}' to 'std::vector<int>::size_type {aka unsigned int}'
我做错了什么?如何打印 table?
operator[]
需要类型为 std::vector::size_type
的参数,因此 iterator
不能用作其参数。您可以将循环更改为:
for (vector<int>::size_type i = 0; i < iter->second.size(); i++)
{
cout<<(iter->second)[i];
}
或者您可以 cout<<*iter1;
打印迭代器指向的值。
for(vector<int> :: const_iterator iter1 = iter->second.begin(); iter1 != iter->second.end(); ++iter1)
{
cout<<*iter1;
}
要从迭代器中获取值,只需使用 *
取消引用它,就像您对指针所做的那样。但是在这里显式地使用迭代器是没有意义的。只需使用基于范围的 for 循环。
for(const auto& p : table)
{
cout << p.first << "\t";
for(const auto& i : p.second)
{
cout << i;
}
cout<<"\n";
}
您可以按以下方式更新代码以打印所有值,
for(auto iter = table.begin(); iter != table.end(); ++iter)
{
cout<<iter->first<< "\t";
for(auto iter1 = iter->second.begin(); iter1 != iter->second.end(); ++iter1)
{
cout<<*iter1;
}
cout<<"\n";
}