vector动态存储

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> a;
    char answer = 'y'; //存储用户的选择
    int i; //存储用户输入的整数
    cout<<"请输入整数:";
    while(cin>>i) {
        a.push_back(i);//像vector中动态添加元素
        cout<<"您要继续吗(y or n):";
        cin >> answer;
        if (answer!='y' && answer!='Y')
            break;
        else
            cout<<"请输入整数:";
    }
    for(auto x:a) //遍历,auto自动推断x类型
        cout<< x << " ";
    cout<<endl;
    return 0;
}
iMac-52:a apple$ ./a.out
请输入整数:12
您要继续吗(y or n):y
请输入整数:0
您要继续吗(y or n):y
请输入整数:8
您要继续吗(y or n):y 
请输入整数:-2
您要继续吗(y or n):n
12 0 8 -2

results matching ""

    No results matching ""