c++ map遍历方法 for循环

引入头文件:

#include 
#include 
#include 
//引入名称空间
using namespace std;

初始化一个map

map columns;
columns["device_serial_num"] = "ARMJ9X1529W01916";
columns["device_number"] = "1号";
columns["username"] = "uuua";
//检查map长度
cout << deviceInfoMap.size() << endl;

遍历map中的内容:

//iter声明应该与 columns的声明一致
//否则会报没有操作匹配的运算符
map::iterator iter;
for (iter = columns.begin(); iter != columns.end(); iter++) {
     cout << " key:" << iter->first //key
            << " -- value:" << iter->second  //value
            << endl;
}

运行结果:

3
 key:device_serial_num -- value:ARMJ9X1529W01916
 key:device_number -- value:1号
 key:username -- value:uuua

你可能感兴趣的:(脱发启示录,c++,gnu)