string vector: push_back() and back()
2013-05-24 17:37 三犬风 阅读(977) 评论(0) 编辑 收藏 举报在解析基于键值对的配置文件时,常常需要将key和value解析出来作为其它函数的两个参数去做进一步的操作。下面的代码演示了如何运用vector<string>作为载体存储解析出来的key和value:
// 解析 key = value std::string input = "key = value"; int tokensize = 0; typedef std::vector<std:string> string_vector_t; string_vector_t token; tokens.push_back(""); // 添加空string "" for (std::string::const_iterator si = input.begin(); si != input.end(); ++si) { if (*si == '=') { tokens.push_back(""); // 添加空string “” } else { tokens.back() += *si; // 在空string后面append字符,该string随着每次append不断的更新 } tokensize = tokens.size(); // tokensize == 2; }
Always go with the choice that scares you the most, because that's the one that is going to require the most from you!