Populating Vector from string Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogi_bear_79

    Populating Vector from string Help

    Below is a snippet of code that works for me. Currenlty it populates
    my vector with one digit per element. I would like to modify this
    code to populate each element of the vector with 3 digits from the
    string.

    (i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
    = 672, block [2] = 534, and block [3] = 179)

    vector<intvOne, vTwo;
    string one, two;
    size_t r;

    cout << "\n\n\t Please enter the first integer:\t";
    cin>>one;

    r = one.length();

    for (int x = 0; x < r; x++){
    vOne.push_back( int(one[x] - '0'));
    }
  • Kai-Uwe Bux

    #2
    Re: Populating Vector from string Help

    yogi_bear_79 wrote:
    Below is a snippet of code that works for me. Currenlty it populates
    my vector with one digit per element. I would like to modify this
    code to populate each element of the vector with 3 digits from the
    string.
    >
    (i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
    = 672, block [2] = 534, and block [3] = 179)
    This example is poorly chosen since it hides an important detail. How do you
    want to store "12345"? Do you want 12.345 or 123.45?

    [snip]


    Best

    Kai-Uwe Bux

    Comment

    • yogi_bear_79

      #3
      Re: Populating Vector from string Help

      On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
      yogi_bear_79 wrote:
      Below is a snippet of code that works for me.  Currenlty it populates
      my vector with one digit per element.  I would like to modify this
      code to populate each element of the vector with 3 digits from the
      string.
      >
      (i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
      = 672, block [2] = 534, and block [3] = 179)
      >
      This example is poorly chosen since it hides an important detail. How do you
      want to store "12345"? Do you want 12.345 or 123.45?
      >
      [snip]
      >
      Best
      >
      Kai-Uwe Bux
      I would want it to be [012] {345]

      Comment

      • Martin York

        #4
        Re: Populating Vector from string Help

        On Mar 16, 6:56 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
        On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
        >
        >
        >
        yogi_bear_79 wrote:
        Below is a snippet of code that works for me. Currenlty it populates
        my vector with one digit per element. I would like to modify this
        code to populate each element of the vector with 3 digits from the
        string.
        >
        (i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
        = 672, block [2] = 534, and block [3] = 179)
        >
        This example is poorly chosen since it hides an important detail. How do you
        want to store "12345"? Do you want 12.345 or 123.45?
        >
        [snip]
        >
        Best
        >
        Kai-Uwe Bux
        >
        I would want it to be [012] {345]
        Accosding to your first post you mean:
        [345]
        [012]

        But will the input include ',' or were they just to help explain?
        ie. Is the input going to be a number or a real string.

        Comment

        • yogi_bear_79

          #5
          Re: Populating Vector from string Help

          On Mar 17, 12:28 am, Martin York <Martin.YorkAma ...@gmail.comwr ote:
          On Mar 16, 6:56 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
          >
          >
          >
          >
          >
          On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
          >
          yogi_bear_79 wrote:
          Below is a snippet of code that works for me.  Currenlty it populates
          my vector with one digit per element.  I would like to modify this
          code to populate each element of the vector with 3 digits from the
          string.
          >
          (i.e. 179,534,672,198 would be stored with block [0] = 198, block [1]
          = 672, block [2] = 534, and block [3] = 179)
          >
          This example is poorly chosen since it hides an important detail. How do you
          want to store "12345"? Do you want 12.345 or 123.45?
          >
          [snip]
          >
          Best
          >
          Kai-Uwe Bux
          >
          I would want it to be [012] {345]
          >
          Accosding to your first post you mean:
          [345]
          [012]
          >
          But will the input include ',' or were they just to help explain?
          ie. Is the input going to be a number or a real string.- Hide quoted text -
          >
          - Show quoted text -
          just a number, no other characters will be present

          Comment

          • James Kanze

            #6
            Re: Populating Vector from string Help

            On Mar 17, 12:48 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
            On Mar 17, 12:28 am, Martin York <Martin.YorkAma ...@gmail.comwr ote:
            On Mar 16, 6:56 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
            On Mar 16, 9:49 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
            yogi_bear_79 wrote:
            Below is a snippet of code that works for me.
            Currenlty it populates my vector with one digit per
            element. I would like to modify this code to populate
            each element of the vector with 3 digits from the
            string.
            (i.e. 179,534,672,198 would be stored with block [0] =
            198, block [1] = 672, block [2] = 534, and block [3] =
            179)
            This example is poorly chosen since it hides an
            important detail. How do you want to store "12345"? Do
            you want 12.345 or 123.45?
            [snip]
            I would want it to be [012] {345]
            Accosding to your first post you mean:
            [345]
            [012]
            But will the input include ',' or were they just to help
            explain? ie. Is the input going to be a number or a real
            string.- Hide quoted text -
            just a number, no other characters will be present
            It's a weird requirement, but:

            std::string::co nst_iterator end = myString.end() ;
            std::string::co nst_iterator begin = myString.begin( ) ;
            while ( end - begin >= 3 ) {
            std::istringstr eam s( std::string( begin, begin + 3 ) ) ;
            int i ;
            s >i ;
            // Error handling goes here...
            myVect.push_bac k( i ) ;
            begin += 3 ;
            }

            In general, when you need to break a string (or any container
            supporting random access iterators) up into smaller blocks, you
            can create an instance using two iterators, which delimit the
            smaller block. And any time you have to convert from or to
            strings, istringstream or ostringstream do the trick.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...