Jump to content

  • Log In with Google      Sign In   
  • Create Account

back_inserter or end()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 KingofNoobs   Members   -  Reputation: 301

Like
0Likes
Like

Posted 28 November 2012 - 01:21 AM

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.

I wonder as I wander...

http://www.davesgameoflife.com


Sponsor:

#2 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
2Likes
Like

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"...

#3 Zipster   Crossbones+   -  Reputation: 679

Like
0Likes
Like

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.

#4 ApochPiQ   Moderators   -  Reputation: 15765

Like
1Likes
Like

Posted 28 November 2012 - 05:51 PM

Alternative suggestion:

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 SiCrane   Moderators   -  Reputation: 9599

Like
0Likes
Like

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 adeyblue   Members   -  Reputation: 518

Like
2Likes
Like

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;





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS