Solved

pop an element

Posted on 2003-11-13
2
223 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
Need 'Configuration File' Management Library for C++ Project 1 495
VS2015 compilation and missing DLLs 9 147
IdTCPClient1->Disconnect(); not working 3 66
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

770 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