Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining a vector of strings

People also ask

Can I make a vector of strings?

A vector of strings is created the way a vector of any other type would be created. Remember to make the template specialization, string. Do not forget to include the string library and the vector library. The common ways of creating vectors with string as the element type have been illustrated above.

How do I concatenate a character vector in R?

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.

How do you combine two strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time. The C# examples in this article run in the Try.NET inline code runner and playground.


Assuming this is question 6.8, it doesn't say you have to use accumulate - it says use "a library algorithm". However, you can use accumulate:

#include <numeric>
    
int main () {
    std::string str = "Hello World!";
    std::vector<std::string> vec(10,str);
    std::string a = std::accumulate(vec.begin(), vec.end(), std::string(""));
    std::cout << a << std::endl;
}

All that accumulate does is set 'sum' to the third parameter, and then for all of the values 'val' from first parameter to second parameter, do:

sum = sum + val

it then returns 'sum'. Despite the fact that accumulate is declared in <numeric> it will work for anything that implements operator+()


Note: This solution, while elegant, is inefficient, as a new string will be allocated and populated for each element of vec.


How about std::copy?

std::ostringstream os;
std::copy( vec_strings.begin(), vec_string.end(), ostream_iterator<string>( os ) );
cout << os.str() << endl;

The following snippet compiles in Visual C++ 2012 and uses a lambda function:

int main () {
    string str = "Hello World!";
    vector<string>  vec (10,str);

    stringstream ss;
    for_each(vec.begin(), vec.end(), [&ss] (const string& s) { cat(ss, s); });

    cout << ss.str() << endl;
}

The accumulate example in the 1st answer is elegant, but as sellibitze pointed out, it reallocates with each concatenation and scales at O(N²). This for_each snippet scales at about O(N). I profiled both solutions with 100K strings; the accumulate example took 23.6 secs, but this for_each snippet took 0.054 sec.


I am not sure about your question.Where lies the problem? Its just a matter of a loop.

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

int main () 
{
    std::string str = "Hello World!";
    std::vector<string>  vec (10,str);

    for(size_t i=0;i!=vec.size();++i)
        str=str+vec[i];
    std::cout<<str;
}

EDIT :

Use for_each() from <algorithm>

Try this:

#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string i;
void func(string &k)
{
  i+=k;
}
int main () {
    string str = "Hello World!";
    vector<string>  vec (10,str);

    for_each(vec.begin(),vec.end(),func);
    cout<<i;
    return 0;
  }