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. string concat

string concat

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
6 Posts 3 Posters 0 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.
  • N Offline
    N Offline
    nuttynibbles
    wrote on last edited by
    #1

    Hi, I understand we can concat strings using + operator. for example, string strarray[20]; string rule= "1 "; string word = "; hello world"; for(int i=0;i<10;i++){ strarray[i] = rule + word; } however if i do this, it prompt an error: string strarray[20]; for(int i=0;i<10;i++){ strarray[i] = "Rule " + "; hello world"; } how do i go about doing this? tks

    T C N 3 Replies Last reply
    0
    • N nuttynibbles

      Hi, I understand we can concat strings using + operator. for example, string strarray[20]; string rule= "1 "; string word = "; hello world"; for(int i=0;i<10;i++){ strarray[i] = rule + word; } however if i do this, it prompt an error: string strarray[20]; for(int i=0;i<10;i++){ strarray[i] = "Rule " + "; hello world"; } how do i go about doing this? tks

      T Offline
      T Offline
      T2102
      wrote on last edited by
      #2

      Either #1 or 2 below should work... You get an error since you the + operator is not defined between two char arrays 1) strarray[i] = std::string("Rule ") + std::string("; hello world"); 2) strarray[i] = "Rule "; strarray[i].append("; hello world");

      N 1 Reply Last reply
      0
      • N nuttynibbles

        Hi, I understand we can concat strings using + operator. for example, string strarray[20]; string rule= "1 "; string word = "; hello world"; for(int i=0;i<10;i++){ strarray[i] = rule + word; } however if i do this, it prompt an error: string strarray[20]; for(int i=0;i<10;i++){ strarray[i] = "Rule " + "; hello world"; } how do i go about doing this? tks

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        nuttynibbles wrote:

        strarray[i] = "Rule " + "; hello world";

        Because in that case you try to concatenate char arrays, not std::string. Thus, there's no + operator overloading for char arrays. The + operator is overloaded by the string object, so it means the left operand of the + has to be a string, not a char array.

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • T T2102

          Either #1 or 2 below should work... You get an error since you the + operator is not defined between two char arrays 1) strarray[i] = std::string("Rule ") + std::string("; hello world"); 2) strarray[i] = "Rule "; strarray[i].append("; hello world");

          N Offline
          N Offline
          nuttynibbles
          wrote on last edited by
          #4

          WOW im very please with the SUPER quick reponse from both of you. TK you so much. im gonna go with the 2nd solution tho :) however, tks to both again!!!!

          1 Reply Last reply
          0
          • N nuttynibbles

            Hi, I understand we can concat strings using + operator. for example, string strarray[20]; string rule= "1 "; string word = "; hello world"; for(int i=0;i<10;i++){ strarray[i] = rule + word; } however if i do this, it prompt an error: string strarray[20]; for(int i=0;i<10;i++){ strarray[i] = "Rule " + "; hello world"; } how do i go about doing this? tks

            N Offline
            N Offline
            nuttynibbles
            wrote on last edited by
            #5

            btw, is there a function in c++ to search for duplicate items in an array without doing a for loop?

            T 1 Reply Last reply
            0
            • N nuttynibbles

              btw, is there a function in c++ to search for duplicate items in an array without doing a for loop?

              T Offline
              T Offline
              T2102
              wrote on last edited by
              #6

              No. You would either use a structure that prevents duplicates (e.g. .NET SortedList in C# --- not sure if there is one in C++) or use qsort, std::sort, and then identify duplicates.

              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