home   articles   tags   browse code   

C++ implode function


 

Here is a quick implode function for C++.

#include <iostream>
#include <string>

using namespace std;

string implode( const string &glue, const vector<string> &pieces );

int main(int argc, char *argv[])
{
    vector<string> v;
    v.push_back("1");
    v.push_back("2");
    v.push_back("3");
    cout << implode(" ", v)<< endl; // "1 2 3"
}

string implode( const string &glue, const vector<string> &pieces )
{
    string a;
    int leng=pieces.size();
    for(int i=0; i<leng; i++)
    {
        a+= pieces[i];
        if (  i < (leng-1) )
            a+= glue;
    }
    return a;
}
 

Tags: c++
 
Aleksandr on Nov 18th, 2010 4:10 am said:
That smell good as hell! Exatly what I was lookin for. :))
 



 

 



Related Articles
 


home  |  privacy policy  |  terms of use  |  contact  


©2012, Zedwood.com

zedwood.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com