From: utab on
Dear all,

I tried to concatenate a vector<string> into one string by using copy
algorithm however my code did not even compile. However I could not
understand why I can not even compile it.

Say,

string str("C++ ");
vector<string> strings(10, str);
string s;
copy(strings.begin(),strings.end(),back_inserter(s));

back_inserter can be used on a string because it is ok to use
push_back on string class. To use back_inserter, the container should
support push_back and strings are also containers with push_back
member function, right?

But I get compile errors. I accomplished that later with accumulate
however but wondered the reason of the above problem.

Rgds,


From: Jerry Coffin on
In article <223827d8-0481-4c24-b4ac-68268f5e79c1
@a9g2000prl.googlegroups.com>, umut.tabak(a)gmail.com says...

[ ... ]

> back_inserter can be used on a string because it is ok to use
> push_back on string class. To use back_inserter, the container should
> support push_back and strings are also containers with push_back
> member function, right?

Yes and no. std::string _does_ have a push_back member function -- but
it takes a charT, not an std::string<charT, ...> You can add individual
character this way, but not entire strings at a time.

--
Later,
Jerry.

The universe is a figment of its own imagination.
From: dasjotre on
On Apr 13, 8:40 pm, utab <umut.ta...(a)gmail.com> wrote:
> Dear all,
>
> I tried to concatenate a vector<string> into one string by using copy
> algorithm however my code did not even compile. However I could not
> understand why I can not even compile it.
>
> Say,
>
> string str("C++ ");
> vector<string> strings(10, str);
> string s;
> copy(strings.begin(),strings.end(),back_inserter(s));
>
> back_inserter can be used on a string because it is ok to use
> push_back on string class. To use back_inserter, the container should
> support push_back and strings are also containers with push_back
> member function, right?
>
> But I get compile errors. I accomplished that later with accumulate
> however but wondered the reason of the above problem.
>
> Rgds,

you can use stringstream to accumulate the result

std::vector<std::string> string_vector_;
std::stringstream ss;
std::copy(string_vector_.begin(), string_vector_.end(),
std::ostream_iterator<std::string>(ss));

std::string result = ss.str();

DS
 | 
Pages: 1
Prev: Update map Object
Next: Array Return