DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C++ String Join

06.24.2009
| 6247 views |
  • submit to reddit
        // description of your code here

string str_join(const vector<string> & vec,const string & sep)
{
        if(vec.size()==0)
                return "";
        string::size_type size=sep.length()*vec.size();
        for(unsigned int i=0;i<vec.size();i++)
        {
                size+=vec[i].size();
        }

        string tmp;
        tmp.reserve(size);
        tmp=vec[0];
        for(unsigned int i=1;i<vec.size();i++)
        {
                tmp=tmp+sep+vec[i];
        }
        return tmp;
}