vector to vector assignment question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divideby0
    New Member
    • May 2012
    • 131

    vector to vector assignment question

    I *think* I've figured it out; std::copy appears to do what I'm looking for.

    Cheers
    -------------------------------------------


    I'm in need of a vector member function that would allow me to "insert" a number of elements from one vector into another vector without resizing or destroying either.

    An example of what I'm wanting to do is assign the contents of two[3-5][50-54] to one[7-9][2-6]. Other than looping through both vectors using .at(), is there a way to copy this?

    This would be continuous within a user controlled loop with differing elements being exchanged.

    Code:
    typedef vector<vector<unsigned char> > vec;
    
    vec one(10, vector<unsigned char>(10, '1')),
        two(90, vector<unsigned char>(90, '2'));
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    std::copy uses iterators. Get an iterator to the start of your target insertion point. Then get an iterator to the first element of your insert element. Then get an iterator to the next element after the last insert element. These last two iterators are your begin and end.

    Those loops you are talking about are inside std::copy.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      As always, thank you for your comments.

      What I am actually doing is reading a data file into the large vector. I have a COORD (c) that corresponds to the current row and col of the large vector (lv). The user alters the row or col.

      The smaller vector (sv) has a static border with just the field elements being copied. I came up with

      Code:
      for(i = (c.X - (sv.size() / 2)) + 1; i < (c.X + (sv.size() / 2)) - 2); ++i)
         for(j = 1; j < sv.size() - 1; ++j)
            copy(lv[i].begin() + (c.Y - (sv[0].size() / 2)),
                 lv[i].begin() + (c.Y + (sv[0].size() / 2)) - 1),
                 sv[j].begin() + 1);
      I say that I think it works because my data file is just filled with 0s right now. It will be populated by varying characters though. The above fills in the field of sv just as I needed. Whether or not it's the right characters has yet to be determined.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You are reading into an array, right? Then you are using that array to populate your vector, yes?

        If so, hard code values in that array and then check to see your vector is correct.

        Comment

        • divideby0
          New Member
          • May 2012
          • 131

          #5
          curses; it looks as though it's swapped rows and cols during the copy. I'm working on another part right now, but will draw it out on paper and see where I've gone wrong.

          ---------------------------------------------------




          I'm using a string and getline to read each line of the file; this is my function to load the file and populate the large vector

          Code:
          bool loadWorld(const std::string &fn, std::ifstream &fh, maps &m)
          {
              int ch;
              unsigned row = 0;
              std::string s;
          
              fh.open(fn.c_str(), std::ios::in);
          
              if(!fh.is_open())
                  return false;
          
              while(row < m.size() && ((ch = fh.peek()) != EOF))
              {
                  std::getline(fh, s);
                  m[row].assign(s.begin(), s.end());
                  ++row;
              }
          
              fh.close();
              return true;
          }
          I coded the function this way because the file name may change frequently loading new data, and I thought it would be easier to pass in the ifstream object rather than open and close it in main().

          here, m is the large vector; maps is the typedef from the first post. maps world(100, vector<unsigned char>(300, ' ')) is the declaration;

          I'll copy a few lines into the large vector and see what happens.

          Comment

          Working...