C++循环通过映射
C++ Loop through Map
我想遍历map<string, int>
中的每个元素,而不知道它的任何字符串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?
}
}
您可以实现以下目标:
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;
}
对于C++11(及以后),
for (auto const& x : symbolTable)
{
std::cout << x.first // string (key)
<< ':'
<< x.second // string's value
<< std::endl;
}
对于C++17(及以后),
for (auto const& [key, val] : symbolTable)
{
std::cout << key // string (key)
<< ':'
<< val // string's value
<< std::endl;
}
尝试以下
for ( const auto &p : table )
{
std::cout << p.first << 't' << p.second << std::endl;
}
同样可以使用普通的for loop 写入
for ( auto it = table.begin(); it != table.end(); ++it )
{
std::cout << it->first << 't' << it->second << std::endl;
}
考虑到std::map
的value_type是通过以下方式定义的
typedef pair<const Key, T> value_type
因此,在我的示例中,p是对value_type的常量引用,其中Key是std::string
,T是int
此外,如果该函数被声明为会更好
void output( const map<string, int> &table );
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';
}
或者使用C++11,使用基于范围的
for (auto const& p : symbolTable)
{
std::cout << p.first << ' ' << p.second << 'n';
}
正如来自莫斯科的@Vlad所说,考虑到std::map
的value_type
定义方式如下:
typedef pair<const Key, T> value_type
这意味着,如果您希望用更明确的类型说明符替换关键字auto
,那么您可以这样做;
for ( const pair<const string, int> &p : table ) {
std::cout << p.first << 't' << p.second << std::endl;
}
只是为了理解auto
在这种情况下会翻译成什么。
由于P0W为每个C++版本提供了完整的语法,我想通过查看您的代码来补充几点
- 始终将
const &
作为参数,以避免同一对象的额外副本 - 使用CCD_ 15,因为它使用起来总是更快。查看此讨论
这是一个示例代码:
#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;
}
它甚至可以用经典的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++;
}
如果您只想在不更改值的情况下迭代内容do:
for(const auto & variable_name : container_name(//here it is map name)){
cout << variable_name.first << " : " << variable_name.second << endl;
}
如果要修改映射的内容,请删除const
并保留&
(如果要直接修改容器内的内容)。如果要使用容器值的副本,也可以删除&
符号;之后,您可以在"上使用.first
和.second
来访问它们;variable_name";。
其他方式:
map <int, string> myMap = {
{ 1,"Hello" },
{ 2,"stackOverflow" }
};
for (auto iter = cbegin(myMap); iter != cend(myMap); ++iter) {
cout << iter->second << endl;
}
相关文章:
- 在映射擦除c++期间执行循环的次数
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- 迭代嵌套映射与范围为循环:没有名为"first"的成员
- 映射中循环的迭代器
- 为什么这个 x 值在这个循环映射中没有增加?C++
- 在循环(C++)中用新指针填充映射
- c++ 在循环中删除迭代器(映射的映射)
- 遍历 STL 映射(集/多集)的最佳方法,同时元素可能会在循环期间被删除并重新插入?
- 映射基于自动的循环单元素访问C++
- 有没有办法在不使用循环的情况下非具体地引用映射中的每个值?C++
- 在 stl 映射和列表 (c++) 上进行迭代的泛型循环
- C++:分段函数,循环初始化为映射
- c++ 过期映射条目线程与事件循环
- 我如何将输入分配给在 for 循环中设置的映射功能
- 循环访问提升mapped_region/内存映射文件
- 复制地图<双精度,元组<双精度,双精度>>映射<双精度,双精度>没有循环?
- 在使用无序多映射实现的有向图中查找循环
- 循环访问映射并删除动态分配的元素
- 不带迭代程序的哈希映射循环
- 如何在映射向量中循环访问地图