【问题标题】:replacing a string in a vector without positioning在没有定位的情况下替换向量中的字符串
【发布时间】:2014-05-22 15:27:49
【问题描述】:

在我现在正在处理的代码中,我从 txt 文件中加载了一个向量,现在我试图查看它们是否是一种在不需要位置或任何东西的情况下替换向量中某些单词的方法
因此,例如,如果 txt 包含动物列表并且我想将鸟更改为预订,我将如何做到这一点而不需要字母的位置

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

vector <string> test;

int main()
{
  string file;
  fstream fout( "Vector.txt" );
  while  ( !fout.eof())
  {
   getline(fout,file);
   test.push_back(file);
  }
  fout.close();


  for( int i = 0; i < test.size(); i++)
  {
   cout << test[i] << endl;
  }


  system("pause");
}

txt 包含:




河马

【问题讨论】:

  • 不清楚你在 test[i] 中有什么(一个词,一个句子?)。
  • 你能发布一个输入 txt 示例和一个可能的输出吗?我不明白你想做什么。
  • 好吧,基本上不用知道我想将txt文件中的单词更改为其他内容的位置

标签: c++ vector fstream


【解决方案1】:

使用std::transform()

std::string bird2book(const string &str)
{
    if (str == "bird")
        return "book";
    return str;
}

std::transform(test.begin(), test.end(), test.begin(), bird2book);

【讨论】:

    【解决方案2】:

    你可以使用std::replace

    std::replace (test.begin(), test.end(), "bird", "book"); 
    

    【讨论】:

      【解决方案3】:

      试试这个:

      typedef std::istream_iterator<string> isitr;
      
      ifstream fin("Vector.txt");
      vector <string> test{ isitr{fin}, isitr{} }; // vector contains strings
      
      map<string,string> dict{ // replacements dictionary
          {"bird", "book"}, {"cat", "kitten"}
      };
      
      for(auto& x: test) // x must be a reference
      {
          auto itr = dict.find(x);
          if(itr != dict.end()) // if a match was found
              x = itr->second; // replace x with the found replacement
                               // (this is why x must be a reference)
      }
      
      for(const auto& x: test)
          cout << test << " ";
      

      【讨论】:

        【解决方案4】:

        使用 STL!!这是我们的力量。您需要的一切:

        #include <iostream>
        #include <algorithm>
        #include <iterator>
        #include <vector>
        #include <string>
        #include <fstream>
        #include <map>
        
        int main()
        {
            std::vector<std::string> words;
        
            const std::map<std::string, std::string> words_to_replace{
                    { "bird", "book" }, { "cat", "table" }
                };
            auto end = words_to_replace.cend();
        
            std::transform(
                std::istream_iterator<std::string>{ std::ifstream{ "file.txt" } },
                std::istream_iterator<std::string>(),
                std::back_inserter(words),
                [&](const std::string& word) {
                    auto word_pos = words_to_replace.find(word);
                    return (word_pos != end) ? word_pos->second : word;
                });
        
            std::copy(words.cbegin(), words.cend(),
                std::ostream_iterator<std::string>(std::cout, "\n"));
            std::cout << std::endl;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-12
          • 2022-01-03
          • 1970-01-01
          • 2010-11-11
          • 2020-10-27
          • 1970-01-01
          • 1970-01-01
          • 2017-04-27
          相关资源
          最近更新 更多