DISTURBED
DISTURBED

Reputation: 63

How can I copy content from one string vector to another?

For example, I have a string-based vector:

vector<string> text_vec;

There are several words stored in each string. So, I need to copy the each word from this vector to another string-vector, but I should place each word in the individual string. How can I do this?

Upvotes: 1

Views: 2081

Answers (2)

Christian Hackl
Christian Hackl

Reputation: 27548

You mean your vector contents look like this?

{ "word0", "word1 word2 word3", "word4 word5" }

And you want to have a result like this:

{ "word0", "word1", "word2", "word3", "word4", "word5" }

The first important thing is to define what constitutes a word. I'll assume a word is everything separated by at least one space. In practice, you may want to handle a few special cases, for example:

  • Empty strings.
  • Other whitespace characters.
  • Newlines.

Let's first define a string-split function that takes a std::string and returns a std::vector<std::string>. It will first provide simple splitting using the aforementioned assumption; you can make it more sophisticated later on:

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

Having this function at our disposal, we can apply it to your input vector:

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

Here's a complete test program:

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

Upvotes: 1

jensa
jensa

Reputation: 2890

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]);
    }
}

Upvotes: 1

Related Questions