A
A
Alexander Taratin2015-12-22 03:56:05
C++ / C#
Alexander Taratin, 2015-12-22 03:56:05

An analogue of QStringBuilder for vanilla c++. Where to get?

Where can I get an analogue of QStringBuilder for vanilla c++?
So that you can also beautifully and efficiently concatenate std:: strings without unnecessary memory allocation.
In style

std::string val = "hello world";
int p = 100500;
std::string result = '[' % val % " : " %  p % ']';

UPD.
I am well aware of std::stringstream. Interested in pleasant syntax.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Taratin, 2015-12-23
@Taraflex

Based on AtomKrieg answer
https://gist.github.com/QW0101/a57e3c59bd7c9c39ff19

S
Stanislav Makarov, 2015-12-22
@Nipheris

std::stringstream
Regarding "extra memory allocation" - it is necessary to take implementations and compare (i.e. I can't say how much QStringBuild is better/worse than std::stringstream), but, as they say, look&feel is almost the same. Replace percentages with << and then call .str()

M
Maxim Moseychuk, 2015-12-22
@fshp

#include <boost/format.hpp>
#include <iostream>

int main() {

        std::string val = "hello world";
        boost::format f("[%s : %i]");
        int p = 100500;

        std::cout << boost::str(f % val % p) << std::endl;
        std::cout << boost::str(f % val % (p - 1)) << std::endl; //Can be reused
        std::cout << boost::str(boost::format("Hello, %s!") % "@Taraflex") << std::endl; //Can be initialized in place

        return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question