Solved

pop an element

Posted on 2003-11-13
2
221 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 70 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 30 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now