Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString Vs std::string

CString Vs std::string

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioquestion
5 Posts 5 Posters 1 Views 1 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    With CString, we can perform an operation something like:

    CString strMarks;
    strMarks.Format("I got %d marks",marks);

    Can we perform similar operation using std::string ??? If yes, then how ???

    N R M 3 Replies Last reply
    0
    • A Anonymous

      With CString, we can perform an operation something like:

      CString strMarks;
      strMarks.Format("I got %d marks",marks);

      Can we perform similar operation using std::string ??? If yes, then how ???

      N Offline
      N Offline
      Nitron
      wrote on last edited by
      #2

      Try this:

      char buffer\[200\] = {0};	
      std::string str = "";
      int i = 9;
      sprintf(buffer, "\\n\\nThis is number: %d\\n\\n", i);
      str = buffer;
      printf(str.c\_str());
      

      - Nitron


      "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

      1 Reply Last reply
      0
      • A Anonymous

        With CString, we can perform an operation something like:

        CString strMarks;
        strMarks.Format("I got %d marks",marks);

        Can we perform similar operation using std::string ??? If yes, then how ???

        R Offline
        R Offline
        Roman Fadeyev
        wrote on last edited by
        #3

        Unfortunately, you can't. In concept of C++ std library you must use streams (like as stdout, stdin and etc). This method was perfectly described by Alexandrescu in his book "Modern C++...". But personally I like to use old good method as Format. And I have invented my own function. May be it will be useful for you

        string_smart Format(cstr szText,...)
        {
        cstr szPtr=szText;
        size_t lLen;
        //Calculating Average length
        for (lLen=0;*szPtr; ++szPtr,(*szPtr=='%') ?lLen+=10:lLen++);

        string\_cstr strRes(++lLen);
        
        va\_list marker;
        va\_start( marker, szText);     
        
        while (\_vsnprintf(strRes.buffer(),strRes.buffer\_size()-1,szText,marker) <0 ) 
        	strRes.reserve(strRes.buffer\_size()\*2);
        
        //\_ASSERTE( \_CrtCheckMemory( ));
        strRes.buffer()\[strRes.buffer\_size()-1\]='\\0';	//Don't remove this line!
        strRes.recalc\_len();						
        								
        
        va\_end( marker );              
        
        return strRes;
        

        }

        This function works with my class string_smart, but it is not hard to remake it for std::string

        1 Reply Last reply
        0
        • A Anonymous

          With CString, we can perform an operation something like:

          CString strMarks;
          strMarks.Format("I got %d marks",marks);

          Can we perform similar operation using std::string ??? If yes, then how ???

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          Yes you can, using the stringstream class:

          #include <string>
          #include <sstream>

          ostringstream strm;
          string strMarks;

          strm << "I got " << marks << " marks" << ends;
          strMarks = strm.str();

          --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

          N 1 Reply Last reply
          0
          • M Michael Dunn

            Yes you can, using the stringstream class:

            #include <string>
            #include <sstream>

            ostringstream strm;
            string strMarks;

            strm << "I got " << marks << " marks" << ends;
            strMarks = strm.str();

            --Mike-- Friday's GoogleFight results: Britney Spears 2,190,000 - Erica Weichers 23 :( 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click! My really out-of-date homepage Sonork-100.19012 Acid_Helm

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            I would just rearange it a little:

            #include <string>
            #include <sstream>
            std::ostringstream strm;
            strm << "I got " << marks << " marks" << std::ends;
            std::string strMarks = strm.str();

            :beer:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups