Solved

pop an element

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 146
C++ Language error 28 314
thread-safe code in c++ 2 128
How to convert MFC APP to Win32 APP. 19 133
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 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