qstring .arg like thing in pure c++



  • as the title suggests, i need a way to format std::string like QString does with .arg(), but i only wanna use c++.

    i know that there's boost class to do that (namely, boost::format) but i don't to attach 3rd party since if i do, the project will become heavy (boost is like 1.2gb)



  • You can look up how QString does it but as you mention, probably boost is the fastest solution. boost::format is header only. No need to link anything. Only the things you use will be added to your program, not the whole 1.2gb



  • well but i still need to choose and include bunch of headers

    is it even worth it?



  • 😓
    It's 1 line in code and 1 line in the project file. Let's no be this lazy, shall we?!



  • how is it 1 line? i added the format.hpp but it has dependencies so i need to add them too

    p.s. i can't be lazy cause i'm copying all the needed header files to my project dir :D

    p.s.2 or i'm doing something worng?



  • @user4592357 said in qstring .arg like thing in pure c++:

    as the title suggests, i need a way to format std::string like QString does with .arg(), but i only wanna use c++.

    i know that there's boost class to do that (namely, boost::format) but i don't to attach 3rd party since if i do, the project will become heavy (boost is like 1.2gb)

    Boost has a tool to extract only needed parts



  • thanks, but seems like all the folder got copied

    here's how i did it:

    bcp.exe --boost=..\..\ format C:\Documents\proj\dependencies\boost_1_65_1\boost
    


  • @user4592357 said in qstring .arg like thing in pure c++:

    or i'm doing something worng?

    Exactly, you are doing the difficult way. Do not copy every file in your folder.

    • just add 1 line in the project file (assuming you use qmake on windows): INCLUDEPATH += C:/Boost/boost_1_65_0
    • re-run qmake
    • add #include<boost/format.hpp> in your source file


  • @VRonin

    the thing is that im not using qt. in that case i'd just use qstring and that's all.



  • @user4592357 The above has nothing to do with Qt, just with the build system you use.

    Since you posted here I supposed you were using Qt Creator.
    If you use visual studio you should just add C:\Boost\boost_1_65_0 to the "Additional include directories" under Project Properies->C/C++->General



  • @VRonin

    i've done that with the whole 1.2gb boost directly. that works

    now i need to send the project to my teacher but i don't want to include 1.2gb of files.

    and how can including just the header work if it includes other files? i still don't get it



  • What program are you using to write, compile and link your binaries?
    What OS are you on?



  • visual studio 2017 on windows 10



  • @VRonin said in qstring .arg like thing in pure c++:

    if you use visual studio you should just add C:\Boost\boost_1_65_0 to the "Additional include directories" under Project Properies->C/C++->General

    Did you try this before copying the 1.2 GB? of course C:\Boost\boost_1_65_0 is just an example and you should point to the folder where you have downloaded boost



  • What about stringstream?
    (http://en.cppreference.com/w/cpp/io/basic_ostringstream)

    I always use something like this

    #include <iostream>
    #include <sstream>
    #include <string>
    
    std::ostringstream mystring;
    std::string someText = "something";
    int number = 11;
    mystring << "This is " << someText << " in pure c++ " << number;
    std::cout << mystring.str();
    


  • @sneubert But it works well only for simple formatting, when you need to print columns or specify precision of floating-point numbers, you have to face the nightmare of ios flags



  • @Konstantin-Tokarev
    yes u are right even though for my use cases the list of manipulators
    (http://en.cppreference.com/w/cpp/io/manip) is sufficient and keep in
    mind that only the brave ones are rewarded :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.