C++ 不定位地替换向量中的字符串

C++ 不定位地替换向量中的字符串,c++,vector,fstream,C++,Vector,Fstream,在我现在正在编写的代码中,我有一个从txt文件加载的向量,现在我试图看看它们是否是一种无需位置或任何东西即可替换向量中某些单词的方法 例如,如果txt包含一个动物列表,我想把bird改为book,我怎么做,而不需要字母的位置 #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; vector <stri

在我现在正在编写的代码中,我有一个从txt文件加载的向量,现在我试图看看它们是否是一种无需位置或任何东西即可替换向量中某些单词的方法
例如,如果txt包含一个动物列表,我想把bird改为book,我怎么做,而不需要字母的位置

#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");
}
#包括
#包括
#包括
#包括
使用名称空间std;
病媒试验;
int main()
{
字符串文件;
fstream fout(“Vector.txt”);
而(!fout.eof())
{
getline(fout,文件);
测试。推回(文件);
}
fout.close();
对于(int i=0;i
使用

使用

使用

你可以用

你可以用

你可以用

你可以用


使用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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
向量词;
const std::将单词映射到替换{
{“鸟”、“书”}、{“猫”、“桌子”}
};
自动结束=单词_to_replace.cend();
std::transform(
std::istream_迭代器{std::ifstream{“file.txt”},
std::istream_迭代器(),
标准::背面插入器(文字),
[&](常量标准::字符串和单词){
自动单词位置=要替换的单词。查找(单词);
返回(word\u pos!=end)?word\u pos->second:word;
});
复制(words.cbegin(),words.cend(),
std::ostream_迭代器(std::cout,“\n”);

std::cout使用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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
向量词;
const std::将单词映射到替换{
{“鸟”、“书”}、{“猫”、“桌子”}
};
自动结束=单词_to_replace.cend();
std::transform(
std::istream_迭代器{std::ifstream{“file.txt”},
std::istream_迭代器(),
标准::背面插入器(文字),
[&](常量标准::字符串和单词){
自动单词位置=要替换的单词。查找(单词);
返回(word\u pos!=end)?word\u pos->second:word;
});
复制(words.cbegin(),words.cend(),
std::ostream_迭代器(std::cout,“\n”);

std::cout使用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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
向量词;
const std::将单词映射到替换{
{“鸟”、“书”}、{“猫”、“桌子”}
};
自动结束=单词_to_replace.cend();
std::transform(
std::istream_迭代器{std::ifstream{“file.txt”},
std::istream_迭代器(),
标准::背面插入器(文字),
[&](常量标准::字符串和单词){
自动单词位置=要替换的单词。查找(单词);
返回(word\u pos!=end)?word\u pos->second:word;
});
复制(words.cbegin(),words.cend(),
std::ostream_迭代器(std::cout,“\n”);

std::cout使用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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
向量词;
const std::将单词映射到替换{
{“鸟”、“书”}、{“猫”、“桌子”}
};
自动结束=单词_to_replace.cend();
std::transform(
std::istream_迭代器{std::ifstream{“file.txt”},
std::istream_迭代器(),
标准::背面插入器(文字),
[&](常量标准::字符串和单词){
自动单词位置=要替换的单词。查找(单词);
返回(word\u pos!=end)?word\u pos->second:word;
});
复制(words.cbegin(),words.cend(),
std::ostream_迭代器(std::cout,“\n”);
std::cout试试这个:

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 << " ";
typedef std::istream_迭代器isitr;
ifstream fin(“Vector.txt”);
向量测试{isitr{fin},isitr{};//向量包含字符串
映射dict{//替换字典
{“鸟”、“书”}、{“猫”、“小猫”}
};
for(auto&x:test)//x必须是引用
{
自动itr=dict.find(x);
if(itr!=dict.end())//如果找到匹配项
x=itr->second;//用找到的替换项替换x
//(这就是为什么x必须是参考)
}
用于(常量自动和x:测试)
试试这个:

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 << " ";
typedef std::istream_迭代器isitr;
ifstream fin(“Vector.txt”);
向量测试{isitr{fin},isitr{};//向量包含字符串
映射dict{//替换字典
{“鸟”、“书”}、{“猫”、“小猫”}
};
for(auto&x:test)//x必须是引用
{
自动itr=dict.find(x);
if(itr!=dict.end())//如果找到匹配项
x=itr->second;//用找到的替换项替换x
//(这就是为什么x必须是参考)
}
用于(常量自动和x:测试)
试试这个:

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 << " ";
typedef std::istream_迭代器isitr;
ifstream fin(“Vector.txt”);
向量测试{isitr{fin},isitr{};//向量包含字符串
映射dict{//替换字典
{“鸟”、“书”}、{“猫”、“小猫”}
};
for(auto&x:test)//x必须是引用
{
自动itr=dict.find(x);
if(itr!=dict.end())//如果找到匹配项
x=itr->second;//用找到的替换项替换x
//(这就是为什么x必须是参考)
}
用于(常量自动和x:测试)
试试这个:

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 << " ";
typedef std::istream_迭代器isitr;
ifstream fin(“Vector.txt”);
向量测试{isitr{fin},isitr{};//向量包含字符串
映射dict{//替换字典
{“鸟”、“书”}、{“猫”、“小猫”}
};
for(auto&x:test)//x必须是引用
{
自动itr=dict.find(x);
if(itr!=dict.end())//如果找到匹配项
x=itr->second;//用找到的替换项替换x
//(这就是为什么x必须是参考)
}
用于(常量自动和x:测试)

测试[i](一个单词,一个句子?)中有哪些内容还不清楚。你能发布一个输入txt示例和一个可能的示例吗