Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

vector<string>

[es] :: C/C++ programiranje :: vector<string>

[ Pregleda: 2936 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

bane

Član broj: 449
Poruke: 230
*.teol.net



Profil

icon vector<string>06.02.2003. u 14:46 - pre 257 meseci
Kompajliram I sve radi, ali kada startujem program I unesem string vector neznem šta treba da unesem da bi program znao da je kraj vektora?

Stack.h

#include <string>
#include <vector>
using namespace std;
class Stack {
public:
bool push( const string& );
bool pop ( string &elem );
bool peek( string &elem );
bool empty() const { return _stack.empty(); }
bool full() const { return _stack.size() == _stack.max_size(); }
int size() const { return _stack.size(); }
private:
vector<string> _stack;
};


Stack.cpp


#include<iostream>
#include "Stack.h"
bool Stack::pop( string &elem ){
if ( empty() ) return false;
elem = _stack.back();
_stack.pop_back();
return true;
}
bool Stack::peek( string &elem ){
if ( empty() ) return false;
elem = _stack.back();
return true;
}
bool Stack::push( const string &elem ){
if ( full() ) return false;
_stack.push_back( elem );
return true;
}
int main() {
Stack st;
string str;
while ( cin >> str && ! st.full() )
st.push( str );
if ( st.empty() ) {
cout << '\n' << "Oops: no strings were read -- bailing out\n ";
return 0;
}
st.peek( str );
if ( st.size() == 1 && str.empty() ) {
cout << '\n' << "Oops: no strings were read -- bailing out\n ";
return 0;
}
cout << '\n' << "Read in " << st.size() << " strings!\n"
<< "The strings, in reverse order: \n";
while ( st.size() )
if ( st.pop( str ))
cout << str << ' ';
cout << '\n' << "There are now " << st.size()
<< " elements in the stack!\n";
}



1000 zasto 1000 zato
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
199.171.112.*



+6 Profil

icon Re: vector<string>06.02.2003. u 16:08 - pre 257 meseci
Unos se prekida kada usov u petlji

while ( cin >> str && ! st.full() )
st.push( str );

ne bude zadovoljen. Znači, kada uneseš nešto što nije string (teorijski), EOF (u Windows-u ovo nije moguće uneti sa tastature - kod Unix-a je Ctrl-D) ili kad je stek pun.

Ja bih na tvom mestu proširio malo uslov:

while ( cin >> str && ! st.full() && str.compare("exit") != 0 )
st.push( str );

Ova petlja će da se prekine kad kucaš "exit".

 
Odgovor na temu

[es] :: C/C++ programiranje :: vector<string>

[ Pregleda: 2936 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.