473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

vector to vector assignment question

131 128KB
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.

Expand|Select|Wrap|Line Numbers
  1. typedef vector<vector<unsigned char> > vec;
  2.  
  3. vec one(10, vector<unsigned char>(10, '1')),
  4.     two(90, vector<unsigned char>(90, '2'));
  5.  
Dec 28 '12 #1
4 2070
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 28 '12 #2
divideby0
131 128KB
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

Expand|Select|Wrap|Line Numbers
  1. for(i = (c.X - (sv.size() / 2)) + 1; i < (c.X + (sv.size() / 2)) - 2); ++i)
  2.    for(j = 1; j < sv.size() - 1; ++j)
  3.       copy(lv[i].begin() + (c.Y - (sv[0].size() / 2)),
  4.            lv[i].begin() + (c.Y + (sv[0].size() / 2)) - 1),
  5.            sv[j].begin() + 1);
  6.  
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.
Dec 29 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 30 '12 #4
divideby0
131 128KB
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

Expand|Select|Wrap|Line Numbers
  1. bool loadWorld(const std::string &fn, std::ifstream &fh, maps &m)
  2. {
  3.     int ch;
  4.     unsigned row = 0;
  5.     std::string s;
  6.  
  7.     fh.open(fn.c_str(), std::ios::in);
  8.  
  9.     if(!fh.is_open())
  10.         return false;
  11.  
  12.     while(row < m.size() && ((ch = fh.peek()) != EOF))
  13.     {
  14.         std::getline(fh, s);
  15.         m[row].assign(s.begin(), s.end());
  16.         ++row;
  17.     }
  18.  
  19.     fh.close();
  20.     return true;
  21. }
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.
Dec 30 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Doug Tolton | last post by:
I have a function that returns a tuple: def checkdoc(self, document): blen = document bates = document normal, truncated, semicolon = 0,0,0 for bat in bates: if len(bat) == 2 * blen:...
0
by: Prune Tracy | last post by:
I have a question about assignment of slices of valarrays. I want to assign one general slice of a valarray to another. It seems the only way is to use the static_cast syntax, but this seems to...
5
by: Csaba2000 | last post by:
I thought assigning one function to another was fairly straightforward, as the example below illustrates. So I was really surprised that IE 6 showed the alert while Firefox 1.0.1 failed. It's...
2
by: needin4mation | last post by:
Hi, in this statement: int num1, *ptr; num1=50; ptr=&num1; *ptr = (*ptr+25); The indirection operator in (*ptr+25) says take the value that is held in the address pointed to *ptr (which...
2
by: Chris Roth | last post by:
When I create a vector with: vector<doublev(10); am I right to assume that it has initialized ten elements with the number zero? What if I want to just make space for 10 elements I'll add...
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
3
by: txtamil2 | last post by:
I have declared a vector like this: typedef vector <intmyvec; 1. myvec vec1; 2. vec1.push_back(4); 3. vec1 = 5; The question is how come line 3 works (no crash). But when I look at the...
5
by: Boltar | last post by:
Hi Is there a way of inserting and erasing to/from a vector using an array index instead of an iterator or alternatively a way to map an index number to an iterator? eg: vector<intv;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.