Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. qstring .arg like thing in pure c++
Forum Updated to NodeBB v4.3 + New Features

qstring .arg like thing in pure c++

Scheduled Pinned Locked Moved Unsolved C++ Gurus
17 Posts 4 Posters 5.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

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

        is it even worth it?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • U Offline
            U Offline
            user4592357
            wrote on last edited by user4592357
            #5

            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?

            VRoninV 1 Reply Last reply
            0
            • K Offline
              K Offline
              Konstantin Tokarev
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              2
              • U Offline
                U Offline
                user4592357
                wrote on last edited by
                #7

                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
                
                1 Reply Last reply
                0
                • U user4592357

                  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?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  @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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  U 1 Reply Last reply
                  2
                  • VRoninV VRonin

                    @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
                    U Offline
                    U Offline
                    user4592357
                    wrote on last edited by user4592357
                    #9

                    @VRonin

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

                    VRoninV 1 Reply Last reply
                    0
                    • U user4592357

                      @VRonin

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

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      U VRoninV 2 Replies Last reply
                      2
                      • VRoninV VRonin

                        @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

                        U Offline
                        U Offline
                        user4592357
                        wrote on last edited by
                        #11

                        @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

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

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

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          1
                          • U Offline
                            U Offline
                            user4592357
                            wrote on last edited by
                            #13

                            visual studio 2017 on windows 10

                            1 Reply Last reply
                            0
                            • VRoninV VRonin

                              @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

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              @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

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              1 Reply Last reply
                              0
                              • sneubertS Offline
                                sneubertS Offline
                                sneubert
                                wrote on last edited by
                                #15

                                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();
                                
                                K 1 Reply Last reply
                                2
                                • sneubertS sneubert

                                  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();
                                  
                                  K Offline
                                  K Offline
                                  Konstantin Tokarev
                                  wrote on last edited by
                                  #16

                                  @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

                                  sneubertS 1 Reply Last reply
                                  1
                                  • K Konstantin Tokarev

                                    @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

                                    sneubertS Offline
                                    sneubertS Offline
                                    sneubert
                                    wrote on last edited by
                                    #17

                                    @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 :-)

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved