449,264 Members | 1,743 Online
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

# STL vector question

 P: n/a Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the values to a 2D vector, of which every vector has the same length. I tried the following but I get a "System.NullReferenceException" error when I ran it. cycle = 2; n = 12; vector< vector >v2D; v2D.reserve(cycle); for (int i = 0; i < cycle; i++) { beginOffset = i * n; endOffset = beginOffset + n; v2D[i].reserve(n); copy(v1.begin() + beginOffset, v1.begin() + endOffset, v2D[i].begin()); } Basically what I am doing is taking n numbers at a time iteratively, and push them to a 2D vector of size 2-by-n. I also tried using back_inserter in place of v2D[i].begin() but it didn't help. Am I doing something wrong? Thanks. Jessica Jul 19 '05 #1
4 Replies

 P: n/a "Jessica" wrote... Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the values to a 2D vector, of which every vector has the same length. I tried the following but I get a "System.NullReferenceException" error when I ran it. cycle = 2; n = 12; vector< vector >v2D; v2D.reserve(cycle); 'reserve' doesn't create elements in that vector. It just assures that a certain number of 'push_back's won't cause reallocation. for (int i = 0; i < cycle; i++) { beginOffset = i * n; endOffset = beginOffset + n; v2D[i].reserve(n); Again, 'reserve' doesn't create elements. You need 'resize'. copy(v1.begin() + beginOffset, v1.begin() + endOffset, v2D[i].begin()); } Basically what I am doing is taking n numbers at a time iteratively, and push them to a 2D vector of size 2-by-n. I also tried using back_inserter in place of v2D[i].begin() but it didn't help. Am I doing something wrong? Thanks. Yes. You're using 'reserve' instead of 'resize'. Victor Jul 19 '05 #2

 P: n/a jl*****@earthlink.net (Jessica) wrote in news:d1**************************@posting.google.c om: Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the values to a 2D vector, of which every vector has the same length. I tried the following but I get a "System.NullReferenceException" error when I ran it. cycle = 2; n = 12; vector< vector >v2D; v2D.reserve(cycle); for (int i = 0; i < cycle; i++) { beginOffset = i * n; endOffset = beginOffset + n; v2D[i].reserve(n); copy(v1.begin() + beginOffset, v1.begin() + endOffset, v2D[i].begin()); } Basically what I am doing is taking n numbers at a time iteratively, and push them to a 2D vector of size 2-by-n. I also tried using back_inserter in place of v2D[i].begin() but it didn't help. Am I doing something wrong? Thanks. Jessica Reserve() doesn't do what you think it does: ..reserve() makes the internal representation of the vector big enough to hold the specified number of objects, but does not actually construct them. Thus .capacity() of the vector will be >= the specified size, but ..size() will still be 0. ..resize() will make the vector big enough the hold the specified number of objects, and will construct that number of objects. Thus .capacity() of the vector will be >= the specified size, and .size() will be the specified size (each object will be default constructed). In your code, you v2D.reserve(2), and then try to access those (unconstructed) objects. You also try to v2D[0].reserve(12), and v2D [1].reserve(12), and try to access those as well. Your back_inserter idea would have worked, but your original v2D.reserve(2) will have already fouled things up. If you replace your calls to .reserve() with calls to .resize() you should be fine (no back_inserter). Jul 19 '05 #3

 P: n/a Ah. Thanks guys. That explains the strange exceptions that I've been getting. So when should reserve() be used? I guess what confused me is this paragraph that I read from SGI: "Reserve() causes a reallocation manually. The main reason for using reserve() is efficiency: if you know the capacity to which your vector must eventually grow, then it is usually more efficient to allocate that memory all at once rather than relying on the automatic reallocation scheme. The other reason for using reserve() is so that you can control the invalidation of iterators." Thank you again! Jessica Andre Kostur wrote in message news:... jl*****@earthlink.net (Jessica) wrote in news:d1**************************@posting.google.c om: Hi, I do not have a lot of experience with STL and I hope some of you might be able to help me on this seemingly elementary question. I have a vector of doubles (v1). I am trying to copy the values to a 2D vector, of which every vector has the same length. I tried the following but I get a "System.NullReferenceException" error when I ran it. cycle = 2; n = 12; vector< vector >v2D; v2D.reserve(cycle); for (int i = 0; i < cycle; i++) { beginOffset = i * n; endOffset = beginOffset + n; v2D[i].reserve(n); copy(v1.begin() + beginOffset, v1.begin() + endOffset, v2D[i].begin()); } Basically what I am doing is taking n numbers at a time iteratively, and push them to a 2D vector of size 2-by-n. I also tried using back_inserter in place of v2D[i].begin() but it didn't help. Am I doing something wrong? Thanks. Jessica Reserve() doesn't do what you think it does: .reserve() makes the internal representation of the vector big enough to hold the specified number of objects, but does not actually construct them. Thus .capacity() of the vector will be >= the specified size, but .size() will still be 0. .resize() will make the vector big enough the hold the specified number of objects, and will construct that number of objects. Thus .capacity() of the vector will be >= the specified size, and .size() will be the specified size (each object will be default constructed). In your code, you v2D.reserve(2), and then try to access those (unconstructed) objects. You also try to v2D[0].reserve(12), and v2D [1].reserve(12), and try to access those as well. Your back_inserter idea would have worked, but your original v2D.reserve(2) will have already fouled things up. If you replace your calls to .reserve() with calls to .resize() you should be fine (no back_inserter). Jul 19 '05 #4