我想遍历map<string, int>中的每个元素,而不知道它的任何string-int值或键。到目前为止,我有:
map<string, int>
void output(map<string, int> table){ map<string, int>::iterator it; for (it = table.begin(); it != table.end(); it++) { //How do I access each element? }}
void output(map<string, int> table)
{
map<string, int>::iterator it;
for (it = table.begin(); it != table.end(); it++)
//How do I access each element?
}
字符串
j8yoct9x1#
你可以像下面这样实现:
map<string, int>::iterator it;for (it = symbolTable.begin(); it != symbolTable.end(); it++){ std::cout << it->first // string (key) << ':' << it->second // string's value << std::endl;}
for (it = symbolTable.begin(); it != symbolTable.end(); it++)
std::cout << it->first // string (key)
<< ':'
<< it->second // string's value
<< std::endl;
字符串对于 C++11(及以后),
for (auto const& x : symbolTable){ std::cout << x.first // string (key) << ':' << x.second // string's value << std::endl;}
for (auto const& x : symbolTable)
std::cout << x.first // string (key)
<< x.second // string's value
型在 C++17(及以后)中,
for (auto const& [key, val] : symbolTable){ std::cout << key // string (key) << ':' << val // string's value << std::endl;}
for (auto const& [key, val] : symbolTable)
std::cout << key // string (key)
<< val // string's value
型
hrysbysz2#
尝试以下
for ( const auto &p : table ){ std::cout << p.first << '\t' << p.second << std::endl;}
for ( const auto &p : table )
std::cout << p.first << '\t' << p.second << std::endl;
字符串同样的代码也可以用普通的for循环来写
for ( auto it = table.begin(); it != table.end(); ++it ){ std::cout << it->first << '\t' << it->second << std::endl;}
for ( auto it = table.begin(); it != table.end(); ++it )
std::cout << it->first << '\t' << it->second << std::endl;
型请注意,std::map的value_type是通过以下方式定义的
std::map
typedef pair<const Key, T> value_type
型因此,在我的示例中,p是对value_type的常量引用,其中Key是std::string,T是int另外,如果将函数声明为
std::string
int
void output( const map<string, int> &table );
nom7f22z3#
map的value_type是一个pair,包含键和值,分别作为first和second的成员。
map
value_type
pair
first
second
map<string, int>::iterator it;for (it = symbolTable.begin(); it != symbolTable.end(); it++){ std::cout << it->first << ' ' << it->second << '\n';}
std::cout << it->first << ' ' << it->second << '\n';
字符串或者使用C++11,使用基于范围的:
for (auto const& p : symbolTable){ std::cout << p.first << ' ' << p.second << '\n';}
for (auto const& p : symbolTable)
std::cout << p.first << ' ' << p.second << '\n';
umuewwlo4#
由于P0W已经为每个C++版本提供了完整的语法,我想通过查看您的代码来添加更多要点
const &
unordered_map
下面是一个示例代码:
#include <iostream>#include <unordered_map>using namespace std;void output(const auto& table){ for (auto const & [k, v] : table) { std::cout << "Key: " << k << " Value: " << v << std::endl; }}int main() { std::unordered_map<string, int> mydata = { {"one", 1}, {"two", 2}, {"three", 3} }; output(mydata); return 0;}
#include <iostream>
#include <unordered_map>
using namespace std;
void output(const auto& table)
for (auto const & [k, v] : table)
std::cout << "Key: " << k << " Value: " << v << std::endl;
int main() {
std::unordered_map<string, int> mydata = {
{"one", 1},
{"two", 2},
{"three", 3}
};
output(mydata);
return 0;
hwamh0ep5#
正如来自莫斯科的@Vlad所说,考虑到std::map的value_type定义如下:
字符串这意味着如果你想用一个更明确的类型说明符来替换关键字auto,那么你可以这样做;
auto
for ( const pair<const string, int> &p : table ) { std::cout << p.first << '\t' << p.second << std::endl;}
for ( const pair<const string, int> &p : table ) {
型只是为了理解在这种情况下auto将转换为什么。
sg24os4d6#
它甚至可以用经典for循环来完成。手动推进迭代器。
for
typedef std::map<int, int> Map;Map mymap;mymap['a']=50;mymap['b']=100;mymap['c']=150;mymap['d']=200;bool itexist = false;int sizeMap = static_cast<int>(mymap.size());auto it = mymap.begin();for(int i = 0; i < sizeMap; i++){ std::cout << "Key: " << it->first << " Value: " << it->second << std::endl; it++;}
typedef std::map<int, int> Map;
Map mymap;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
bool itexist = false;
int sizeMap = static_cast<int>(mymap.size());
auto it = mymap.begin();
for(int i = 0; i < sizeMap; i++){
std::cout << "Key: " << it->first << " Value: " << it->second << std::endl;
it++;
vngu2lb87#
如果你只是想在不改变值的情况下覆盖内容,请执行:
for(const auto & variable_name : container_name(//here it is map name)){ cout << variable_name.first << " : " << variable_name.second << endl; }
for(const auto & variable_name : container_name(//here it is map name)){
cout << variable_name.first << " : " << variable_name.second << endl;
字符串如果你想修改map的内容,删除const并保留&(如果你想直接修改容器内的内容)。如果你想使用容器值的 * 副本 *,也删除&符号;之后,你可以通过在“variable_name”上使用.first和.second来访问它们。
const
&
.first
.second
t2a7ltrp8#
其他方式:
map <int, string> myMap = { { 1,"Hello" }, { 2,"stackOverflow" }};for (auto iter = cbegin(myMap); iter != cend(myMap); ++iter) { cout << iter->second << endl;}
map <int, string> myMap = {
{ 1,"Hello" },
{ 2,"stackOverflow" }
for (auto iter = cbegin(myMap); iter != cend(myMap); ++iter) {
cout << iter->second << endl;
8条答案
按热度按时间j8yoct9x1#
你可以像下面这样实现:
字符串
对于 C++11(及以后),
型
在 C++17(及以后)中,
型
hrysbysz2#
尝试以下
字符串
同样的代码也可以用普通的for循环来写
型
请注意,
std::map
的value_type是通过以下方式定义的型
因此,在我的示例中,p是对value_type的常量引用,其中Key是
std::string
,T是int
另外,如果将函数声明为
型
nom7f22z3#
map
的value_type
是一个pair
,包含键和值,分别作为first
和second
的成员。字符串
或者使用C++11,使用基于范围的:
型
umuewwlo4#
由于P0W已经为每个C++版本提供了完整的语法,我想通过查看您的代码来添加更多要点
const &
作为参数,以避免同一对象的额外副本。unordered_map
,因为它总是使用起来更快。参见this discussion下面是一个示例代码:
字符串
hwamh0ep5#
正如来自莫斯科的@Vlad所说,考虑到
std::map
的value_type
定义如下:字符串
这意味着如果你想用一个更明确的类型说明符来替换关键字
auto
,那么你可以这样做;型
只是为了理解在这种情况下
auto
将转换为什么。sg24os4d6#
它甚至可以用经典
for
循环来完成。手动推进迭代器。
字符串
vngu2lb87#
如果你只是想在不改变值的情况下覆盖内容,请执行:
字符串
如果你想修改map的内容,删除
const
并保留&
(如果你想直接修改容器内的内容)。如果你想使用容器值的 * 副本 *,也删除&
符号;之后,你可以通过在“variable_name”上使用.first
和.second
来访问它们。t2a7ltrp8#
其他方式:
字符串