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. ATL / WTL / STL
  4. Sorting out of a vector of strings

Sorting out of a vector of strings

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsalgorithms
7 Posts 5 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.
  • B Offline
    B Offline
    Bernhard
    wrote on last edited by
    #1

    i have got a vector of strings. i would like to sort out all the empty strings.. any neater way to do this than thisstruct _IsEmpty { operator () (const std::string& s) { return s.empty(); } }; void TransformAbtVStr (std::vector& v_str) { v_str.erase (std::remove_if (v_str.begin(), v_str.end(), _IsEmpty()), v_str.end()); }
    Thanks in advance, Bernhard


    "Just looking for loopholes." W. C. Fields
    American actor, 1880-1946, explaining why he was reading the Bible on his deathbed.

    W 1 Reply Last reply
    0
    • B Bernhard

      i have got a vector of strings. i would like to sort out all the empty strings.. any neater way to do this than thisstruct _IsEmpty { operator () (const std::string& s) { return s.empty(); } }; void TransformAbtVStr (std::vector& v_str) { v_str.erase (std::remove_if (v_str.begin(), v_str.end(), _IsEmpty()), v_str.end()); }
      Thanks in advance, Bernhard


      "Just looking for loopholes." W. C. Fields
      American actor, 1880-1946, explaining why he was reading the Bible on his deathbed.

      W Offline
      W Offline
      WREY
      wrote on last edited by
      #2

      vector vecStr; vector::iterator iterB, iterE; // put stuff in your vector of strings iterB = vecStr.begin(); iterE = vecStr.end(); sort(iterB, iterE); vecStr.erase(unique(iterB, iterE), iterE); vecStr.resize(vecStr.size()); ;) William Fortes in fide et opere!

      L 1 Reply Last reply
      0
      • W WREY

        vector vecStr; vector::iterator iterB, iterE; // put stuff in your vector of strings iterB = vecStr.begin(); iterE = vecStr.end(); sort(iterB, iterE); vecStr.erase(unique(iterB, iterE), iterE); vecStr.resize(vecStr.size()); ;) William Fortes in fide et opere!

        L Offline
        L Offline
        LeSurvenant
        wrote on last edited by
        #3

        I'm curious, what is the reason to call the resize method at the end? David

        W 1 Reply Last reply
        0
        • L LeSurvenant

          I'm curious, what is the reason to call the resize method at the end? David

          W Offline
          W Offline
          WREY
          wrote on last edited by
          #4

          Because after you've done the erasing of entries, the size of the vector is no longer what it used to be. Therefore, in order to get it to the new size it has become, you'll need to resize it. And that new size is what "size()" is going to give. Therefore you'll be resizing it to the new size of "size()". ;) William Fortes in fide et opere!

          J 1 Reply Last reply
          0
          • W WREY

            Because after you've done the erasing of entries, the size of the vector is no longer what it used to be. Therefore, in order to get it to the new size it has become, you'll need to resize it. And that new size is what "size()" is going to give. Therefore you'll be resizing it to the new size of "size()". ;) William Fortes in fide et opere!

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #5

            If that is the case for you, then you have a faulty STL implementation. Erase on sequences (and I am prepared to bet money that this is the case for other containter types too), destroys the elements properly and then removes them from the container. The docs says (SGI STL docs): Destroys the elements in the range [p,q) and removes them from a. and: a.size() is decremented by the distance from i to j. The relative order of the other elements in the sequence is unchanged. -- Ich bin Joachim von Hassel, und ich bin Pilot der Bundeswehr. Welle: Erdball - F104-G Starfighter

            W 1 Reply Last reply
            0
            • J Jorgen Sigvardsson

              If that is the case for you, then you have a faulty STL implementation. Erase on sequences (and I am prepared to bet money that this is the case for other containter types too), destroys the elements properly and then removes them from the container. The docs says (SGI STL docs): Destroys the elements in the range [p,q) and removes them from a. and: a.size() is decremented by the distance from i to j. The relative order of the other elements in the sequence is unchanged. -- Ich bin Joachim von Hassel, und ich bin Pilot der Bundeswehr. Welle: Erdball - F104-G Starfighter

              W Offline
              W Offline
              WREY
              wrote on last edited by
              #6

              Sometimes what the documentation says, and what is implemented, are not always the same. You do whatever works. William Fortes in fide et opere!

              P 1 Reply Last reply
              0
              • W WREY

                Sometimes what the documentation says, and what is implemented, are not always the same. You do whatever works. William Fortes in fide et opere!

                P Offline
                P Offline
                palbano
                wrote on last edited by
                #7

                So you are saying that if you log the size before and after the resize() you can provide the proof that they are not equal? Please do.

                cout << vecStr.size() << endl;
                vecStr.resize(vecStr.size());
                cout << vecStr.size() << endl;

                "No matter where you go, there your are." - Buckaroo Banzai

                -pete

                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