Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi,
 
I have a 2D vector and want only the last column to change each time. I want the previous row to be copied over (with 1 less element).
 
Basically if i have an 2D vector visualised as:
 
1 2 3 4
. . . . <---- I want this line to be 2 3 4 (?) where the ? is a value inputted by other means.
 
So if I had it working it would be like this:
 
1 2 3 4
2 3 4 20
3 4 20 5
4 20 5 6
 
so user inputted last value (done)
How do I copy the previous row's values (except the first value) over? I've tried copy ( ) and MyVector.insert( ) ... but the only way I can get working is using a for loop which takes several lines. Can I not use a function like insert when it involves the same vector being copied? Many thanks.
P.S. The first line is inputted before I try the insert.
Posted 7-Dec-12 4:06am
Comments
André Kraak at 7-Dec-12 10:24am
   
You might want to consider using something different like a list, which should make what you want to do easier.
lostandconfused1234 at 7-Dec-12 10:27am
   
Yes, well I have it working using a while loop so:
while(n < NumColumns-1) *inserts the data*,
I'm just trying to optimise my code into the shortest number of lines possible. Somewhat interesting as to why I can't do:
while (file != eof) {
MyVector.insert ( MyVector[i], Myvector[i][1], MyVector[i].end() );
i++
}
lostandconfused1234 at 7-Dec-12 10:28am
   
forgot my manners - thank you though :D
Quirkafleeg at 7-Dec-12 11:49am
   
A deque should be the preferred option to a list.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

An std::list could pop'n'push at its both sides Smile | :) :
typedef std::list<int> listYours;
typedef std::auto_ptr<listYours> ptrYours;
 
void ProduceShiftedListByValue(const ptrYours& listExist, ptrYours& listNew, int iValue)
{
  listNew.reset(listExist.get() ?
                new listYours(*listExist.get()) : // assumed std::list has a copy constructor :)
                NULL);
  if (listNew.get()) {
    if (listNew->size()) {
      listNew->pop_front();
    }
    listNew->push_back(iValue);
  }
}
 
typedef std::list<ptrYours> 2dYours;
// or: typedef std::list<listyours&> 2dYours;
// or: typedef std::list<listyours> 2dYours;
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 250
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,177
2 Maciej Los 3,774
3 Peter Leow 3,478
4 CHill60 2,702


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100