简体   繁体   中英

C++ How to use vector class to create collection

How i can use vector class to create collection? I have to create collection of people's using vector class. I've tried these, but how i can "push_back" a variable?

using namespace std;
int main()
{
    string a,b,c,d,e,f,g;
    cout <<"Name: ";
    cin >>a;
    cout <<"Surname: ";
    cin >>b;
    cout <<"Gender: ";
    cin >>c;
    cout <<"ID: ";
    cin >>d;
    cout <<"Date of birth: ";
    cin >>e;
    cout <<"Street: ";
    cin >>f;
    cout <<"School: ";
    cin >>g;
    std::vector < int > dane;
    dane.push_back( a );
    dane.push_back( b );
    dane.push_back( c );
    dane.push_back( d );
    dane.push_back( e );
    dane.push_back( f );
    dane.push_back( g );

    for( size_t i = 0; i < dane.size(); i++ )
         cout <<dane[ i ];

    cout<<"End";
    return 0;
}

Correct answer

    using namespace std;
int main()
{
    string a,b,c,d,e,f,g;
    cout <<"Name: ";
    cin >>a;
    cout <<"Surname: ";
    cin >>b;
    cout <<"Gender: ";
    cin >>c;
    cout <<"ID: ";
    cin >>d;
    cout <<"Date of birth: ";
    cin >>e;
    cout <<"Street: ";
    cin >>f;
    cout <<"School: ";
    cin >>g;

    std::vector < string > dane;
    dane.push_back( a );
    dane.push_back( b );
    dane.push_back( c );
    dane.push_back( d );
    dane.push_back( e );
    dane.push_back( f );
    dane.push_back( g );

    for( size_t i = 0; i < dane.size(); i++ )
         cout <<dane[ i ]<<endl;

    cout<<"End";
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM