拆分std::string并插入std::set 根据C++聊天室中的怪人的要求,什么是一个好方法来分解一个文件(在我的例子中包含一个大约100行的字符串,每行大约有10个单词),并把所有这些词插入到STD::SET/ < P>最简单的方法是从一个包含一系列元素的源构建任何容器,是使用接受一对迭代器的构造函数。使用istream\u迭代器在流上迭代 #include <set> #include <iostream> #include <string> int main() { std::string temp, mystring; std::set<std::string> myset; while(std::getline(std::cin, temp)) mystring += temp + ' '; temp = ""; for (size_t i = 0; i < mystring.length(); i++) { if (mystring.at(i) == ' ' || mystring.at(i) == '\n' || mystring.at(i) == '\t') { myset.insert(temp); temp = ""; } else { temp.push_back(mystring.at(i)); } } if (temp != " " || temp != "\n" || temp != "\t") myset.insert(temp); for (std::set<std::string>::iterator i = myset.begin(); i != myset.end(); i++) { std::cout << *i << std::endl; } return 0; } #include <set> #include <iostream> #include <string> #include <algorithm> #include <iterator> using namespace std; int main() { //I create an iterator that retrieves `string` objects from `cin` auto begin = istream_iterator<string>(cin); //I create an iterator that represents the end of a stream auto end = istream_iterator<string>(); //and iterate over the file, and copy those elements into my `set` set<string> myset(begin, end); //this line copies the elements in the set to `cout` //I have this to verify that I did it all right copy(myset.begin(), myset.end(), ostream_iterator<string>(cout, "\n")); return 0; } #包括 #包括 #包括 #include

拆分std::string并插入std::set 根据C++聊天室中的怪人的要求,什么是一个好方法来分解一个文件(在我的例子中包含一个大约100行的字符串,每行大约有10个单词),并把所有这些词插入到STD::SET/ < P>最简单的方法是从一个包含一系列元素的源构建任何容器,是使用接受一对迭代器的构造函数。使用istream\u迭代器在流上迭代 #include <set> #include <iostream> #include <string> int main() { std::string temp, mystring; std::set<std::string> myset; while(std::getline(std::cin, temp)) mystring += temp + ' '; temp = ""; for (size_t i = 0; i < mystring.length(); i++) { if (mystring.at(i) == ' ' || mystring.at(i) == '\n' || mystring.at(i) == '\t') { myset.insert(temp); temp = ""; } else { temp.push_back(mystring.at(i)); } } if (temp != " " || temp != "\n" || temp != "\t") myset.insert(temp); for (std::set<std::string>::iterator i = myset.begin(); i != myset.end(); i++) { std::cout << *i << std::endl; } return 0; } #include <set> #include <iostream> #include <string> #include <algorithm> #include <iterator> using namespace std; int main() { //I create an iterator that retrieves `string` objects from `cin` auto begin = istream_iterator<string>(cin); //I create an iterator that represents the end of a stream auto end = istream_iterator<string>(); //and iterate over the file, and copy those elements into my `set` set<string> myset(begin, end); //this line copies the elements in the set to `cout` //I have this to verify that I did it all right copy(myset.begin(), myset.end(), ostream_iterator<string>(cout, "\n")); return 0; } #包括 #包括 #包括 #include,c++,C++,假设您已将文件读入字符串,boost::split将实现以下功能: #include <set> #include <boost/foreach.hpp> #include <boost/algorithm/string.hpp> std::string astring = "abc 123 abc 123\ndef 456 def 456"; // your string std::set<std::string> tokens;

假设您已将文件读入字符串,boost::split将实现以下功能:

#include <set>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>

std::string astring = "abc 123 abc 123\ndef 456 def 456";  // your string
std::set<std::string> tokens;                              // this will receive the words
boost::split(tokens, astring, boost::is_any_of("\n "));    // split on space & newline

// Print the individual words
BOOST_FOREACH(std::string token, tokens){
    std::cout << "\n" << token << std::endl;
}
#包括
#包括
#包括
std::string astring=“abc 123 abc 123\ndef 456 def 456”//你的绳子
std::设置令牌;//这将收到文字
boost::split(令牌、astring、boost::是(“\n”)中的任意一个);//在空格和换行符上拆分
//打印单个单词
BOOST_FOREACH(标准::字符串令牌,令牌){

我不知道你说的动词“to index”是什么意思。也许你的意思是“并将所有这些单词插入std::set?”@尼科:另一方面,这段代码是惯用的;如果我在真实的生产代码中看到德里斯的代码,我会挠头想,为什么他们会采取如此冗长的方法,并可能最终以这种方式重写它。@Lukaschmelzeisen:
ostringstream
正是为了这个目的而设计的。它产生了一个类似于
字符串>ostream
@Drise:很抱歉,这段代码并不混乱或高级。@Drise:一旦您更加熟悉迭代器的使用和
标题,这段代码就比您的代码更容易理解了。@Drise:这怎么可能更容易阅读呢?您对流进行迭代并将其放入容器中(当您从流中读取字符串时,将检索一个单词,因此我们正在读取单词并将其放入一个集合中)这比代码更容易阅读和理解。虽然Dirse的代码更冗长,但它似乎比让< C++ > >代码>运行的速度要快得多,比让<代码> <代码>对我来说要做的太多了。这应该是3行长。你正在编写C,并使用几个C++容器。这就是我们所指的。s C和类。@Lukaschmelzeisen:如果这是真的,那么你就做错了。@LokiAstari:我的名字中只有一个“n”:P,我同意,从我看来,我的代码应该比Drise的快得多。这是一个基于Drise的版本,应该比我没有真正理由编写的两个答案都快:
#include <set>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>

std::string astring = "abc 123 abc 123\ndef 456 def 456";  // your string
std::set<std::string> tokens;                              // this will receive the words
boost::split(tokens, astring, boost::is_any_of("\n "));    // split on space & newline

// Print the individual words
BOOST_FOREACH(std::string token, tokens){
    std::cout << "\n" << token << std::endl;
}