Vector Manipulation

How can I 'pop off' the first element of a vector?

Page 1 of 1

4 Replies - 3466 Views - Last Post: 26 November 2009 - 05:46 AM Rate Topic: -----

#1 Vermiculus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Vector Manipulation

Posted 25 November 2009 - 02:26 PM

void DisplayVector( vector<string> args )
{
	cout << args[0] << "\t" << args[1] << endl;
}

void DisplayAllVectors( vector<vector<string>> args )
{
	for each (vector<string> vect in args)
	{
		DisplayVector(vect);
	}
}


That code seems to work well, but there is a semantic error in it somewhere. I don't know why, but when I run the code it seems to simply cycle through the master vector... It's easier to see what I mean when it runs.. I have attached the source code to the entire program, but the problem definitely lies somewhere in those two functions.

I have attempted to erase vect (args) after I am done with it in DisplayVect, but that ends up with a nasty error. Logically, I shouldn't have to erase it at all since it should move on to the next one in DisplayAllVectors, but whoever said computers are logical...harhar

View PostVermiculus, on 25 Nov, 2009 - 01:23 PM, said:

I have attached the source code to the entire program, but the problem definitely lies somewhere in those two functions.


For some reason it did not attached, so I am trying again... hopefully it'll work... :P


By the way, it is possible to jerryrig the error and make it work by replacing it with the following
int derp = 0;
void DisplayVector( vector<string> args )
{
	cout << args[derp] << "\t" << args[derp+1] << endl;
	derp++;
}

void DisplayAllVectors( vector<vector<string>> args )
{
	for each (vector<string> vect in args)
	{
		DisplayVector(vect);
	}
}


But it's still confusing me why this error would occur in the first place.

Attached File(s)


This post has been edited by Vermiculus: 25 November 2009 - 02:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Vector Manipulation

#2 GWatt   User is offline

  • member icon

Reputation: 309
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Vector Manipulation

Posted 25 November 2009 - 03:06 PM

Your question doesn't seem to have anything to do with popping elements. When you pop a data structure, most likely a stack, you remove and return the last element you 'pushed' on to it.
vector vec;
for (int i = 0; i < 9; i++)
	vec.push_back(i);

int a = vec.pop_back();
// will get 9 as that was the last item 'pushed' on


Your problem is that your multidimensional vector is not printing properly. I would suggest using nested for loops in a single function instead of using different functions to print out the different layers of the vector.

This post has been edited by GWatt: 25 November 2009 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 Silhouette   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 25-November 09

Re: Vector Manipulation

Posted 25 November 2009 - 03:47 PM

If you want to pop from the front, you need a queue or deque data structure.
Plus, if all you're trying to do is display the elements in a vector, you could just cycle through the elements in a for loop, i.e.
cout << myVector[i] << endl;
.
It also has the added benefit of being non-destructive, so you can display your vector elements AND keep using it.
Was This Post Helpful? 0
  • +
  • -

#4 Vermiculus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 314
  • Joined: 26-February 09

Re: Vector Manipulation

Posted 25 November 2009 - 10:25 PM

Ah, I probably should not have used the word pop. I realize that could be confusing ..

And yes, I am definitely over complicating this (which technically is the point of our project :P), but at this point I am simply curious: how can you destroy the first element of a vector so that the latter vector elements are bumped up:

//semipsuedo
vector<int> vect;
vect.push_back(int1);
vect.push_back(int2);
vect.push_back(int3);
//vect now contains [int1, int2, int3]
vect.magicalFunctionKill(int1); // or an index or some kind of demonstrative
//vect now contains [int2, int3]



Is it possible to reverse the order of the vector, like a string.reverse()? Then I could use pop_back and then reverse it again...
Was This Post Helpful? 0
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Vector Manipulation

Posted 26 November 2009 - 05:46 AM

the easiest way is to use the vector::erase function. it takes an iterator which points at the element you want to erase. the first element will be the same as vector::begin, so you can do
vect.erase( vect.begin() ); 
Though, as others have remarked, its easier and (usually) more efficient to use a std::deque ("deque" means Double-Ended Queue) instead of a std::vector if you want to push/pop at both ends. The only thing you lose in a deque is the guarantee that elements are stored contiguously like an array- behind the scenes, a deque is normally "paged" instead - but semantically you retain all the benefits of a vector (notably random access).


as for reversing the order of a vector, you can use an STL algorithm
#include <algorithm>
    // etc..
    std::reverse( vect.begin(), vect.end() ); 

This post has been edited by Bench: 26 November 2009 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1