C++ 迭代到映射中的值字符串集 #包括 使用名称空间std; void insertValue(map和myMap, 字符串常量和键, 字符串常量和值) { //检查是否已经存在给定密钥的集合。 //如果是,请插入到现有集。 //否则,创建一个集合并将其添加到地图中。 迭代器find=myMap.find(键); if(find!=myMap.end()) { cout
迭代C++ 迭代到映射中的值字符串集 #包括 使用名称空间std; void insertValue(map和myMap, 字符串常量和键, 字符串常量和值) { //检查是否已经存在给定密钥的集合。 //如果是,请插入到现有集。 //否则,创建一个集合并将其添加到地图中。 迭代器find=myMap.find(键); if(find!=myMap.end()) { cout,c++,c++11,C++,C++11,迭代映射的最简单方法是使用基于范围的for而不是使用迭代器 #include <iostream> using namespace std; void insertValue(map<string, set<string> >& myMap, string const& key, string const& value) { // Check whether there is already a set gi
映射的最简单方法是使用基于范围的for
而不是使用迭代器
#include <iostream>
using namespace std;
void insertValue(map<string, set<string> >& myMap,
string const& key,
string const& value)
{
// Check whether there is already a set given the key.
// If so, insert to the existing set.
// Otherwise, create a set and add it to the map.
map<string, set<string> >::iterator found = myMap.find(key);
if (found != myMap.end())
{
cout << "Adding '" << value << "' to an existing set of " << key << "s.\n";
found->second.insert(value);
}
else
{
cout << "Adding '" << value << "' to a new set of " << key << "s.\n";
set<string> temp;
temp.insert(value);
myMap.insert(make_pair(key, temp));
}
}
int main()
{
map<string, set<string> > filemap;
insertValue(mymap, "file1", "path1");
insertValue(mymap, "file1", "path2");
insertValue(mymap, "file1", "path3");
insertValue(mymap, "file2", "path1");
insertValue(mymap, "file3", "path2");
return 0;
}
除此之外,插入值的函数可以简化很多。在插入值之前,不需要检查map
中是否已经存在元素,因为map::operator[]
将构造您传递给它的键(如果该键不存在),并且相应的值类型将被值初始化。因此您的insertValue
函数将成为一个单行程序
for(auto miter = mymap.cbegin(); miter != mymap.cend(); ++miter) {
for(auto siter = miter->second.cbegin(); siter != miter->second.cend(); ++siter) {
std::cout << miter->first << ": " << *siter << '\n';
}
}
void insertValue(map和myMap,
字符串常量和键,
字符串常量和值)
{
myMap[key].insert(value);//默认为新键构造一个集合
}
最后,除非您需要对键对应的值进行排序,否则可以使用。此容器类似于映射
,但可以有多个值对应于单个键值。但是,与您的解决方案不同,具有相同键的值的顺序是其插入顺序
迭代映射的最简单方法是使用基于范围的for
而不是使用迭代器
#include <iostream>
using namespace std;
void insertValue(map<string, set<string> >& myMap,
string const& key,
string const& value)
{
// Check whether there is already a set given the key.
// If so, insert to the existing set.
// Otherwise, create a set and add it to the map.
map<string, set<string> >::iterator found = myMap.find(key);
if (found != myMap.end())
{
cout << "Adding '" << value << "' to an existing set of " << key << "s.\n";
found->second.insert(value);
}
else
{
cout << "Adding '" << value << "' to a new set of " << key << "s.\n";
set<string> temp;
temp.insert(value);
myMap.insert(make_pair(key, temp));
}
}
int main()
{
map<string, set<string> > filemap;
insertValue(mymap, "file1", "path1");
insertValue(mymap, "file1", "path2");
insertValue(mymap, "file1", "path3");
insertValue(mymap, "file2", "path1");
insertValue(mymap, "file3", "path2");
return 0;
}
除此之外,插入值的函数可以简化很多。在插入值之前,不需要检查map
中是否已经存在元素,因为map::operator[]
将构造您传递给它的键(如果该键不存在),并且相应的值类型将被值初始化。因此您的insertValue
函数将成为一个单行程序
for(auto miter = mymap.cbegin(); miter != mymap.cend(); ++miter) {
for(auto siter = miter->second.cbegin(); siter != miter->second.cend(); ++siter) {
std::cout << miter->first << ": " << *siter << '\n';
}
}
void insertValue(map和myMap,
字符串常量和键,
字符串常量和值)
{
myMap[key].insert(value);//默认为新键构造一个集合
}
最后,除非您需要对键对应的值进行排序,否则可以使用。此容器类似于映射
,但可以有多个值对应于单个键值。但是,与您的解决方案不同,具有相同键的值的顺序是其插入顺序
迭代映射的最简单方法是使用基于范围的for
而不是使用迭代器
#include <iostream>
using namespace std;
void insertValue(map<string, set<string> >& myMap,
string const& key,
string const& value)
{
// Check whether there is already a set given the key.
// If so, insert to the existing set.
// Otherwise, create a set and add it to the map.
map<string, set<string> >::iterator found = myMap.find(key);
if (found != myMap.end())
{
cout << "Adding '" << value << "' to an existing set of " << key << "s.\n";
found->second.insert(value);
}
else
{
cout << "Adding '" << value << "' to a new set of " << key << "s.\n";
set<string> temp;
temp.insert(value);
myMap.insert(make_pair(key, temp));
}
}
int main()
{
map<string, set<string> > filemap;
insertValue(mymap, "file1", "path1");
insertValue(mymap, "file1", "path2");
insertValue(mymap, "file1", "path3");
insertValue(mymap, "file2", "path1");
insertValue(mymap, "file3", "path2");
return 0;
}
除此之外,插入值的函数可以简化很多。在插入值之前,不需要检查map
中是否已经存在元素,因为map::operator[]
将构造您传递给它的键(如果该键不存在),并且相应的值类型将被值初始化。因此您的insertValue
函数将成为一个单行程序
for(auto miter = mymap.cbegin(); miter != mymap.cend(); ++miter) {
for(auto siter = miter->second.cbegin(); siter != miter->second.cend(); ++siter) {
std::cout << miter->first << ": " << *siter << '\n';
}
}
void insertValue(map和myMap,
字符串常量和键,
字符串常量和值)
{
myMap[key].insert(value);//默认为新键构造一个集合
}
最后,除非您需要对键对应的值进行排序,否则可以使用。此容器类似于映射
,但可以有多个值对应于单个键值。但是,与您的解决方案不同,具有相同键的值的顺序是其插入顺序
迭代映射的最简单方法是使用基于范围的for
而不是使用迭代器
#include <iostream>
using namespace std;
void insertValue(map<string, set<string> >& myMap,
string const& key,
string const& value)
{
// Check whether there is already a set given the key.
// If so, insert to the existing set.
// Otherwise, create a set and add it to the map.
map<string, set<string> >::iterator found = myMap.find(key);
if (found != myMap.end())
{
cout << "Adding '" << value << "' to an existing set of " << key << "s.\n";
found->second.insert(value);
}
else
{
cout << "Adding '" << value << "' to a new set of " << key << "s.\n";
set<string> temp;
temp.insert(value);
myMap.insert(make_pair(key, temp));
}
}
int main()
{
map<string, set<string> > filemap;
insertValue(mymap, "file1", "path1");
insertValue(mymap, "file1", "path2");
insertValue(mymap, "file1", "path3");
insertValue(mymap, "file2", "path1");
insertValue(mymap, "file3", "path2");
return 0;
}
除此之外,插入值的函数可以简化很多。在插入值之前,不需要检查map
中是否已经存在元素,因为map::operator[]
将构造您传递给它的键(如果该键不存在),并且相应的值类型将被值初始化。因此您的insertValue
函数将成为一个单行程序
for(auto miter = mymap.cbegin(); miter != mymap.cend(); ++miter) {
for(auto siter = miter->second.cbegin(); siter != miter->second.cend(); ++siter) {
std::cout << miter->first << ": " << *siter << '\n';
}
}
void insertValue(map和myMap,
字符串常量和键,
字符串常量和值)
{
myMap[key].insert(value);//默认为新键构造一个集合
}
最后,除非您需要对键对应的值进行排序,否则可以使用。此容器类似于映射
,但可以有多个值对应于单个键值。但是,与您的解决方案不同,具有相同键的值的顺序是其插入顺序