Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pop an element

Posted on 2003-11-13
2
Medium Priority
?
230 Views
Last Modified: 2010-04-01
Hi C++ experts,

   I have a code as follows :
-------------------------------------
typedef std::vector<std::string> V ;
typedef std::vector<V> W ;

void insert(W& c, V& item)
{ c.push_back(V()) ; }

int main()
{
  W w1;
  V v1, v2 ;
  v1.push_back("Hello") ;
  v1.push_back("World") ;

  v2.push_back("Doggy_1") ;
  v2.push_back("Doggy_2") ;
  v2.push_back("Doggy_3") ;

  insert(w1,v1) ;
  insert(w1,v2) ;

  return 0 ;
}
-------------------------------------------
  After the data are inserted into w1, I don't know how to pop out
 v1 and v2 ! I don't know how to pop out the element such as "Hello","World", "Doggy_1" ...etc. Could anyone give me some help please ?

  Thanks.

meow.
0
Comment
Question by:meow00
2 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 280 total points
ID: 9743763
Hi meow00,

First of all your insert function is not working properly.
You're always inserting a new V element and not the one passed as a parameter

Down here I posted a code that iterates first through all the W vector and the all through the V vectors in it.
(The ******* are just to know where the each V vector starts

The output would be

********************************
Hello
World
********************************
Doggy_1
Doggy_2
Doggy_3

Hope this helps

Tincho



using namespace std;

typedef vector<string> V ;
typedef vector<V> W ;

void insert(W& c, V& item)
{
      c.push_back( item ) ;   // Here you must insert the item passed as a parameter
}


int main()
{
      W w1;
      V v1, v2 ;
      v1.push_back("Hello") ;
      v1.push_back("World") ;

      v2.push_back("Doggy_1") ;
      v2.push_back("Doggy_2") ;
      v2.push_back("Doggy_3") ;

      insert(w1,v1) ;
      insert(w1,v2) ;


      // Here *it iterates through the elements of the W1 vector
      for( W::const_iterator it = w1.begin(); it != w1.end(); ++it )
      {
            cout << "********************************" << endl;

            V element = (*it);

            // Here *itInternal iterates through the string elements of each of the V vectors
            for( V::const_iterator itInternal = element.begin(); itInternal != element.end(); ++itInternal )
            {
                  cout << (*itInternal) << endl;
            }
      }

      system( "PAUSE" );

      return 0 ;
}


Cheers!
0
 
LVL 15

Assisted Solution

by:efn
efn earned 120 total points
ID: 9743835
You can also just refer to elements with subscripts, e.g., w1[0][0] is "Hello", and w1[1][2] is "Doggy_3".

--efn
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question