C++ 如何将内容从一个字符串向量复制到另一个字符串向量?
例如,我有一个基于字符串的向量:C++ 如何将内容从一个字符串向量复制到另一个字符串向量?,c++,string,vector,copy,C++,String,Vector,Copy,例如,我有一个基于字符串的向量: vector<string> text_vec; vector text\u vec; 每个字符串中存储了几个单词。所以,我需要将每个单词从这个向量复制到另一个字符串向量,但我应该将每个单词放在单独的字符串中。我如何才能做到这一点?矢量文本\u vec\u 2; vector<string> text_vec_2; for(unsigned int i=0;i<text_vec.size();++i){ // as
vector<string> text_vec;
vector text\u vec;
每个字符串中存储了几个单词。所以,我需要将每个单词从这个向量复制到另一个字符串向量,但我应该将每个单词放在单独的字符串中。我如何才能做到这一点?矢量文本\u vec\u 2;
vector<string> text_vec_2;
for(unsigned int i=0;i<text_vec.size();++i){
// assuming a split-function which you have created
// which returns a vector with the individual words
vector<string> words = splitString(text_vec[i]);
// copy the words into the new vector
for(unsigned int j=0;j<words.size();++j){
text_vec_2.push_back(words[j]);
}
}
对于(unsigned int i=0;i您的意思是向量内容如下所示
{ "word0", "word1 word2 word3", "word4 word5" }
你想得到这样的结果:
{ "word0", "word1", "word2", "word3", "word4", "word5" }
第一件重要的事情是定义一个单词的构成。我假设一个单词是由至少一个空格分隔的所有东西。在实践中,您可能需要处理一些特殊情况,例如:
- 空字符串
- 其他空白字符
- 新行
我们首先定义一个字符串拆分函数,该函数接受一个std::string
,并返回一个std::vector
。它将首先使用上述假设提供简单的拆分;您可以在以后使其更复杂:
std::vector<std::string> split(std::string const& input)
{
std::vector<std::string> result;
std::istringstream is(input);
std::string word;
while (is >> word)
{
result.push_back(word);
}
return result;
}
std::向量拆分(std::字符串常量和输入)
{
std::向量结果;
std::istringstream是(输入);
字符串字;
while(is>>word)
{
结果:推回(word);
}
返回结果;
}
利用此函数,我们可以将其应用于您的输入向量:
std::vector<std::string> normalise(std::vector<std::string> const& strings)
{
std::vector<std::string> result;
for (auto const& string : strings)
{
auto const tokens = split(string);
for (auto const& token : split(string))
{
result.push_back(token);
}
}
return result;
}
std::向量归一化(std::向量常量和字符串)
{
std::向量结果;
用于(自动常量和字符串:字符串)
{
自动常量标记=拆分(字符串);
用于(自动常量和标记:拆分(字符串))
{
结果。推回(令牌);
}
}
返回结果;
}
下面是一个完整的测试程序:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
std::vector<std::string> split(std::string const& input)
{
std::vector<std::string> result;
std::istringstream is(input);
std::string word;
while (is >> word)
{
result.push_back(word);
}
return result;
}
std::vector<std::string> normalise(std::vector<std::string> const& strings)
{
std::vector<std::string> result;
for (auto const& string : strings)
{
auto const tokens = split(string);
for (auto const& token : split(string))
{
result.push_back(token);
}
}
return result;
}
int main()
{
std::vector<std::string> const input = { "word0", "word1 word2 word3", "word4 word5" };
for (auto const& word : normalise(input))
{
std::cout << word << "\n";
}
}
#包括
#包括
#包括
#包括
标准::向量拆分(标准::字符串常量和输入)
{
std::向量结果;
std::istringstream是(输入);
字符串字;
while(is>>word)
{
结果:推回(word);
}
返回结果;
}
标准::向量归一化(标准::向量常量和字符串)
{
std::向量结果;
用于(自动常量和字符串:字符串)
{
自动常量标记=拆分(字符串);
用于(自动常量和标记:拆分(字符串))
{
结果。推回(令牌);
}
}
返回结果;
}
int main()
{
向量常量输入={“word0”,“word1 word2 word3”,“word4 word5”};
用于(自动常量和字:归一化(输入))
{
std::每个字符串中的单词可以用一些常用字符分隔吗?他不是指复制向量,而是指文本向量中的每个字符串都由几个单词组成。在新向量中,他希望每个单词都是一个单独的向量元素。单词用几个空格分隔。