5

根据 C++ 聊天室中出色的家伙的要求,什么是分解文件的好方法(在我的例子中,它包含一个大约 100 行的字符串,每行大约 10 个单词)并插入所有这些单词进入 std::set?

4

3 回答 3

25

从包含一系列该元素的源构造任何容器的最简单方法是使用带有一对迭代器的构造函数。用于istream_iterator迭代流。

#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;
}

http://ideone.com/iz1q0

于 2012-06-21T20:43:23.980 回答
3

假设您已将文件读入字符串, 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;
}

如有必要,可以使用列表或向量代替 Set。

另请注意,这几乎是一个骗局: Split a string in C++?

于 2012-06-26T16:07:49.383 回答
2
#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;
}

让我们从顶部开始。首先,您需要使用一些变量。temp只是字符串的占位符,而您从要解析的字符串中的每个字符构建它。mystring是您要拆分的字符串,myset也是您要粘贴拆分字符串的位置。

然后我们读取文件(通过<管道输入)并将内容插入到mystring.

现在我们要向下迭代字符串的长度,搜索空格、换行符或制表符来分割字符串。如果我们找到其中一个字符,那么我们需要insert将字符串放入集合中,并清空占位符字符串,否则,我们将字符添加到占位符中,这将构建字符串。完成后,我们需要将最后一个字符串添加到集合中。

最后,我们遍历集合,并打印每个字符串,这只是为了验证,但在其他情况下可能有用。

编辑:Loki Astari在我认为应该将其集成到答案中的评论中对我的代码进行了重大改进:

#include <set>
#include <iostream>
#include <string>

int main()
{
  std::set<std::string> myset;
  std::string word;

  while(std::cin >> word)
  {
      myset.insert(std::move(word));
  }

  for(std::set<std::string>::const_iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << *it << '\n';
}
于 2012-06-21T20:41:06.740 回答