Aqu*_*irl 2 c++ dictionary iterator stl
map <int, string> rollCallRegister;
map <int, string> :: iterator rollCallRegisterIter;
map <int, string> :: iterator temporaryRollCallRegisterIter;
rollCallRegisterIter = rollCallRegister.begin ();
tempRollCallRegisterIter = rollCallRegister.insert (rollCallRegisterIter, pair <int, string> (55, "swati"));
rollCallRegisterIter++;
tempRollCallRegisterIter = rollCallRegister.insert (rollCallRegisterIter, pair <int, string> (44, "shweta"));
rollCallRegisterIter++;
tempRollCallRegisterIter = rollCallRegister.insert (rollCallRegisterIter, pair <int, string> (33, "sindhu"));
// Displaying contents of this map.
cout << "\n\nrollCallRegister contains:\n";
for (rollCallRegisterIter = rollCallRegister.begin(); rollCallRegisterIter != rollCallRegister.end(); ++rollCallRegisterIter)
{
cout << (*rollCallRegisterIter).first << " => " << (*rollCallRegisterIter).second << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
rollCallRegister contains:
33 => sindhu
44 => shweta
55 => swati
Run Code Online (Sandbox Code Playgroud)
我已经增加了迭代器。为啥还在整理呢?如果位置应该由地图自行更改,那么提供迭代器的目的是什么?
因为std::map是一个排序的关联容器。
在映射中,键值通常用于唯一标识元素,而映射值是与该键关联的某种值。
根据这里的位置参数是
插入操作要比较的第一个元素的位置。请注意,这不会强制新元素位于地图容器内的该位置(集合中的元素始终遵循特定的顺序),但这实际上是容器中可能的插入位置的指示,如果设置为位于元素实际插入位置之前的元素可以实现非常高效的插入操作。iterator 是一个成员类型,定义为双向迭代器类型。
所以这个参数的目的主要是通过缩小元素范围来稍微提高插入速度。
std::vector<std::pair<int,std::string>>如果插入顺序很重要,则可以使用。