Solved

pop an element

Posted on 2003-11-13
2
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

627 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