用于字符串的容器和用于匹配和计数的int型容器
Container for a string and int for matching and counting?
在c++中,创建一个临时容器来存储字符串和int,就像c#字典那样,我可以很容易地将字符串键与另一个字符串匹配,并增加或减少输入值int,最简单的方法是什么?
容器的内容将来自一个字符串,其中每个由空格分隔的单词是一个键项,每个键的所有值都以0开头。
Dictionary<string, int> Options = new Dictionary<string, int>();
Options.Add("xyz", 0);
Options.Add("abc", 0);
Options.Add("dfg", 0);
然后我必须将它与user选项进行比较,例如:
if (Options.ContainsKey(user_opt))
Options[user_opt]++;
我最初尝试使用向量,但由于我对c++的了解几乎为0,所以我基本上卡住了。
对于向量,这是我得到的
vector<string> Options;
boost::split(Options, m_StartMode, boost::is_any_of(" "));
使用std::map
或std::unordered_map
存储键、值
std::map<std::string, int> Options;
Options.insert(std::make_pair("xyz", 0));
Options.insert(std::make_pair("abc", 1));
然后用map::find
检查key是否存在:
std::map<std::string,int>::iterator iter;
iter = Options.find(user_opt);
if(iter != Options.end())
iter->second++;
看std::map
相关文章:
- 运算符 += 应用于 std::valarray<int*>
- C ++ txt 文件成数组逐行字符串和 int 用于纸牌游戏
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 为什么基于 int 的访问不适用于 std::get(std::tuple)?
- 正确语法,用于统一初始化名称中带有空格的类型的临时,如unsigned int
- 如何证明 -> 在 int* pMember = &(pSomeType->SomeIntMember) 时不用于顺从;
- 用于检查"is int"/"is double"/等的模板函数
- sqlite3 更改函数回调参数 void* not用于 int* C++
- 为什么现代C 仍然保留旧的C样式原型,用于int argc,char ** argv
- 是否有任何用于ConstexPR操作员 (INT)的应用程序(INT)
- 双指针 (int**) 和双括号,用于从 2D 数组中获取值
- OpenCL 的 clEnqueueReadBufferRect 适用于 int 但不适用于 double 数据类型
- 使用4用于循环的4矩阵上的数组下标的“ int [int]类型”
- 检测输入键的输入键,用于INT类型
- 无法获得 boost::spirit parser&lexer 适用于 std::string 或 int 或 double 以外的令牌类型
- map(long long int,long long int)不适用于key=1000 000 000(在long l
- C 不是运算符应用于 int
- 3d矢量算术模板(如何专门用于int和float版本)
- 将enum和string组合成一个类似map的变量,可以同时用于int和string
- sizeof 如何适用于 int 类型