Hello,
Is there anything wrong with this:
string temp;
for(auto i = dest.begin(); i != dest.end(); ++i)
{
temp.insert(temp.end(), *i);
}
wcout << fieldTitle << "\t" << temp << endl;
I also tried:
string temp;
for(auto i = dest.begin(); i != dest.end(); ++i)
{
temp.insert(back_inserter(temp), *i);
}
wcout << fieldTitle << "\t" << temp << endl;
but it won't compile. Bjarne Stroustroup says to use back_inserters in his book, but the end() version has been working just fine for me. I was wondering if I didn't understand something.
5 replies to this topic
Ad:
#2 Crossbones+ - Reputation: 1035
Posted 28 November 2012 - 05:40 AM
Back_inserter automatically creates new elements at the back when you assign to it. Many algorithms which otherwise overwrite a range of target elements will instead insert new elements when you use back_inserter.
Your example code is a simple append operation. The cleanest way to do it would be to use the copy algorithm:
copy(begin(wherefrom), end(wherefrom), back_inserter(whereto));
BTW, your example has curious naming, you read items from "dest"...
Your example code is a simple append operation. The cleanest way to do it would be to use the copy algorithm:
copy(begin(wherefrom), end(wherefrom), back_inserter(whereto));
BTW, your example has curious naming, you read items from "dest"...
#3 Crossbones+ - Reputation: 395
Posted 28 November 2012 - 04:56 PM
You can't use an std::back_inserter with a string to append other strings, because std::string doesn't implement the correct interface (it only has a push_back for individual characters, not strings). If you think of a string as a container of characters, then it makes sense that you can only insert characters (since that's what the container holds).
To append strings, you should use either append() or the += operator. insert() also works, but it's a little harder to read and you have the aforementioned shortcuts to make this specific case easier to handle.
To append strings, you should use either append() or the += operator. insert() also works, but it's a little harder to read and you have the aforementioned shortcuts to make this specific case easier to handle.
#4 Moderators - Reputation: 7470
Posted 28 November 2012 - 05:51 PM
Alternative suggestion:
[edit] Oh, screw it. The long empty string should be an escaped t (for tab, as in the OP) but the forums keep unescaping it.
std::wostringstream complete; for(const std::wstring& element : container_of_strings) complete << element; wcout << fieldTitle << L" " << complete.str() << std::endl;
[edit] Oh, screw it. The long empty string should be an escaped t (for tab, as in the OP) but the forums keep unescaping it.
Edited by ApochPiQ, 28 November 2012 - 05:53 PM.
#5 Moderators - Reputation: 6621
Posted 28 November 2012 - 05:59 PM
Is the code tag doing that too?
std::wostringstream complete;
for(const std::wstring& element : container_of_strings)
complete << element;
wcout << fieldTitle << L"\t" << complete.str() << std::endl;
edit: look like not. One more reason to prefer code tags over the source tags.
Edited by SiCrane, 28 November 2012 - 06:00 PM.
#6 Members - Reputation: 515
Posted 28 November 2012 - 07:21 PM
You could also (ab)use the fact that accumulate is implemented in terms of addition and use that to concat a bunch of strings in a container. The need to provide the starting point takes away some elegance points but it's still serviceable.
// include <numeric> std::wcout << fieldTitle << L'\t' << std::accumulate(dest.begin(), dest.end(), std::wstring()) << std::endl;






