best efficient and readable way to concatenate strings (or the best trade-offs) - c++

This is a discussion on best efficient and readable way to concatenate strings (or the best trade-offs) - c++ ; Since C++ (and STL) have many ways to do string concatenation, I want to hear (read) from you how you do to concatenate strings with other strings and other types. The approaches I know are: -- ostringstream this does the ...

+ Reply to Thread
Results 1 to 6 of 6

best efficient and readable way to concatenate strings (or the best trade-offs)

  1. Default best efficient and readable way to concatenate strings (or the best trade-offs)

    Since C++ (and STL) have many ways to do string concatenation, I want
    to hear (read) from you how you do to concatenate strings with other
    strings and other types. The approaches I know are:

    -- ostringstream
    this does the job well, but:
    * all types involved have to support operator<<
    * we will lose some readibility in the code because we will always
    have to create a temp object to do the concatenation

    int n;
    float x;
    ....
    ostringstream temp;
    temp << "we bought " << n << " items at " << x << " price";
    notebook.add(temp.str());
    // three liner


    -- string:perator+
    most advantage is we can built all the string using a one liner and
    passing the result to string const &, but:
    * we have to ensure all members are of string type
    * to achieve that, we can use boost::lexical_cast or custom functions
    to convert our types/classes to string

    int n;
    float x;
    ....
    notebook.add("we bought " + toString(n) + " items at " + toString(x) +
    " price");
    // one liner!!

    (but is there actually a gain doing that?)

    and we have to implement toString(). which way is better?
    lexical_cast? ostringstream? overloading (or specializing) for each
    involved types? (ints and floats can be converted faster using some
    hacks)


    -- ostringstream wrapped in a (family) of functions
    this technique combines the stream versatility with the oneliner
    concatenation thing, but without RVO, I am not sure we will gain
    anything:

    int n;
    float x;
    ....
    notebook.add(concat("we bought ",n," items at ",x," price"));
    // one liner!!

    template<typename A, typename B, typename C, typename D>
    string concat(A a, B b, C c, D d) {
    ostringstream temp;
    temp << a << b << c << d;
    return temp.str();
    }

    this works, but one does not need to be a genius to figure out we have
    to define a family of concat functions to handle the different number
    of parameters

    any ideas?

    thanks!!!

    Diego
    HP


  2. Default Re: best efficient and readable way to concatenate strings (or thebest trade-offs)

    Diego Martins wrote:
    > Since C++ (and STL) have many ways to do string concatenation, I want
    > to hear (read) from you how you do to concatenate strings with other
    > strings and other types. The approaches I know are:

    [...]
    > any ideas?


    Just another way:
    boost.format <http://www.boost.org/libs/format/index.html>

    Use what is most readable and optimize when your profiler says so.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html

  3. Default Re: best efficient and readable way to concatenate strings (or thebest trade-offs)

    On 2007-06-18 22:32, Diego Martins wrote:
    > Since C++ (and STL) have many ways to do string concatenation, I want
    > to hear (read) from you how you do to concatenate strings with other
    > strings and other types. The approaches I know are:


    [snip ostringstream]

    > -- string:perator+
    > most advantage is we can built all the string using a one liner and
    > passing the result to string const &, but:
    > * we have to ensure all members are of string type
    > * to achieve that, we can use boost::lexical_cast or custom functions
    > to convert our types/classes to string
    >
    > int n;
    > float x;
    > ...
    > notebook.add("we bought " + toString(n) + " items at " + toString(x) +
    > " price");
    > // one liner!!
    >
    > (but is there actually a gain doing that?)
    >
    > and we have to implement toString(). which way is better?
    > lexical_cast? ostringstream? overloading (or specializing) for each
    > involved types? (ints and floats can be converted faster using some
    > hacks)


    If you are mainly concerned with user defined types you can implement

    std::string operator+(const std::string&, const myType&)
    and
    std::string operator+(const char*, const MyType&)

    which would make it possible to write things like:

    std::cout << "Value: " + myVal + "\n";

    where myVal is of type MyType.

    --
    Erik Wikström

  4. Default Re: best efficient and readable way to concatenate strings (or thebest trade-offs)

    Diego Martins wrote:
    > Since C++ (and STL) have many ways to do string concatenation, I want
    > to hear (read) from you how you do to concatenate strings with other
    > strings and other types. The approaches I know are:
    >
    > -- ostringstream
    > this does the job well, but:
    > * all types involved have to support operator<<
    > * we will lose some readibility in the code because we will always
    > have to create a temp object to do the concatenation
    >
    > int n;
    > float x;
    > ...
    > ostringstream temp;
    > temp << "we bought " << n << " items at " << x << " price";
    > notebook.add(temp.str());
    > // three liner
    >
    >
    > -- string:perator+
    > most advantage is we can built all the string using a one liner and
    > passing the result to string const &, but:
    > * we have to ensure all members are of string type
    > * to achieve that, we can use boost::lexical_cast or custom functions
    > to convert our types/classes to string
    >
    > int n;
    > float x;
    > ...
    > notebook.add("we bought " + toString(n) + " items at " + toString(x) +
    > " price");
    > // one liner!!
    >
    > (but is there actually a gain doing that?)
    >
    > and we have to implement toString(). which way is better?
    > lexical_cast? ostringstream? overloading (or specializing) for each
    > involved types? (ints and floats can be converted faster using some
    > hacks)
    >
    >
    > -- ostringstream wrapped in a (family) of functions
    > this technique combines the stream versatility with the oneliner
    > concatenation thing, but without RVO, I am not sure we will gain
    > anything:
    >
    > int n;
    > float x;
    > ...
    > notebook.add(concat("we bought ",n," items at ",x," price"));
    > // one liner!!
    >
    > template<typename A, typename B, typename C, typename D>
    > string concat(A a, B b, C c, D d) {
    > ostringstream temp;
    > temp << a << b << c << d;
    > return temp.str();
    > }
    >
    > this works, but one does not need to be a genius to figure out we have
    > to define a family of concat functions to handle the different number
    > of parameters
    >
    > any ideas?
    >
    > thanks!!!
    >
    > Diego
    > HP
    >


    I'm a fan of classes like the following:
    #include <sstream>
    #include <string>

    class concat
    {
    public:
    template <typename T>
    explicit concat(const T & t)
    {
    m_out << t ;
    }

    template <typename T>
    concat & operator()(const T & t)
    {
    m_out << t ;
    return *this ;
    }

    std::string str() const
    {
    return m_out.str() ;
    }
    private:
    std:stringstream m_out ;
    } ;

    #include <iostream>

    int main()
    {
    int n = 4 ;
    float x = .99 ;
    std::string test = concat("We bought ")(n)(" items at ")(x)("
    price").str() ;
    std::cout << test << std::endl ;
    }

  5. Default Re: best efficient and readable way to concatenate strings (or thebest trade-offs)

    Diego Martins wrote:
    > Since C++ (and STL) have many ways to do string concatenation, I want
    > to hear (read) from you how you do to concatenate strings with other
    > strings and other types. The approaches I know are:
    >
    > -- ostringstream
    > this does the job well, but:
    > * all types involved have to support operator<<
    > * we will lose some readibility in the code because we will always
    > have to create a temp object to do the concatenation
    >
    > int n;
    > float x;
    > ...
    > ostringstream temp;
    > temp << "we bought " << n << " items at " << x << " price";
    > notebook.add(temp.str());
    > // three liner
    >
    >
    > -- string:perator+
    > most advantage is we can built all the string using a one liner and
    > passing the result to string const &, but:
    > * we have to ensure all members are of string type
    > * to achieve that, we can use boost::lexical_cast or custom functions
    > to convert our types/classes to string
    >
    > int n;
    > float x;
    > ...
    > notebook.add("we bought " + toString(n) + " items at " + toString(x) +
    > " price");
    > // one liner!!
    >
    > (but is there actually a gain doing that?)
    >
    > and we have to implement toString(). which way is better?
    > lexical_cast? ostringstream? overloading (or specializing) for each
    > involved types? (ints and floats can be converted faster using some
    > hacks)
    >
    >
    > -- ostringstream wrapped in a (family) of functions
    > this technique combines the stream versatility with the oneliner
    > concatenation thing, but without RVO, I am not sure we will gain
    > anything:
    >
    > int n;
    > float x;
    > ...
    > notebook.add(concat("we bought ",n," items at ",x," price"));
    > // one liner!!
    >
    > template<typename A, typename B, typename C, typename D>
    > string concat(A a, B b, C c, D d) {
    > ostringstream temp;
    > temp << a << b << c << d;
    > return temp.str();
    > }
    >
    > this works, but one does not need to be a genius to figure out we have
    > to define a family of concat functions to handle the different number
    > of parameters
    >
    > any ideas?
    >
    > thanks!!!
    >
    > Diego
    > HP
    >


    (Apologies if this message comes through twice. I'm having server
    troubles.)

    I'm a fan of the following syntax for operations that can take an
    indefinite number of parameters:
    #include <sstream>
    #include <string>

    class concat
    {
    public:
    template <typename T>
    explicit concat(const T & t)
    {
    m_out << t ;
    }

    template <typename T>
    concat & operator()(const T & t)
    {
    m_out << t ;
    return *this ;
    }

    std::string str() const
    {
    return m_out.str() ;
    }
    private:
    std:stringstream m_out ;
    } ;

    #include <iostream>

    int main()
    {
    int n = 4 ;
    float x = .99 ;
    std::string test = concat("We bought ")(n)(" items at ")(x)("
    price").str() ;
    std::cout << test << std::endl ;
    }

    --
    Alan Johnson

  6. Default Re: best efficient and readable way to concatenate strings (or the best trade-offs)

    On Jun 18, 6:18 pm, Alan Johnson <a...@yahoo.com> wrote:
    > I'm a fan of the following syntax for operations that can take an
    > indefinite number of parameters:

    [snip]
    > int main()
    > {
    > int n = 4 ;
    > float x = .99 ;
    > std::string test = concat("We bought ")(n)(" items at ")(x)("
    > price").str() ;
    > std::cout << test << std::endl ;
    > }


    thanks for all responses. the above approach is very nice. I liked the
    boost::format too (seems to be very powerful)

    I believe the syntax above could be improved to something like this:
    std::string test = concat("we bought") + n + " items at " + x + "
    price";

    concat class must have a templated operator+() as a member function
    template<typename T>
    concat & concat:perator+(T value) { stream << value; return *this; }

    and an operator string to retrieve/compute the result
    string concat:perator string(); { return stream.str(); }


    if optimization is an issue, we might find a way to use a vector of
    pointers (and typeinfos? o__O) to concat all members at a time in the
    operator string (a custom concatenation? a reserve and vector of
    chars? it could be trickier than one can imagine..)

    more ideas?
    Diego


+ Reply to Thread

Similar Threads

  1. Efficient way to split an array of strings.
    By Application Development in forum Idl-pvwave
    Replies: 3
    Last Post: 11-15-2007, 10:13 AM
  2. print hex strings in readable ascii format
    By Application Development in forum awk
    Replies: 1
    Last Post: 08-16-2007, 08:28 AM
  3. Column Count -vs- Table Count performance trade-offs?
    By Application Development in forum Inetserver
    Replies: 2
    Last Post: 08-01-2007, 02:30 AM
  4. Efficient visualization models for bone and muscular strings
    By Application Development in forum Graphics
    Replies: 0
    Last Post: 03-07-2007, 03:51 AM
  5. [C] Concatenate null-terminated strings? How?
    By Application Development in forum c++
    Replies: 16
    Last Post: 02-21-2007, 08:49 AM