string vector

P87

vector<T> vl               v1是一个空vector,它潜在的元素是T类型的,执行默认初始化

vector<T> v2(vl)           v2中包含有v1所有元索的副本
vector<T> v2=v1            等价于v2 (vl ),v2中包含有 vl所有元素的副木
vector<T> v3(n, val)       v3包含了n个重复的元素,每个元素的值都是val
vector<T> v4(n)            v4包含Tn个重复地执行T值#A始化的对象

vector<T> v5{a,b,c....}    v5包含了初始值个数的元素,侮个元素被赋予相应的初始值

vector<T> v5={a,b,c...}    等价于v5{a,b,c...}

练习3.14:编写一段程序,用cin读入一组整数并把它们存入一个vector对象。

#include <iostream>
#include <vector>


using namespace std;


int main()
{
int intV;
vector<int> text;
char YN;
while (cin >> intV)
{
text.push_back(intV);
cout << "你是否要继续?" << endl;
cin >> YN;
if (YN != 'y' && YN != 'Y')
{
break;
}
}
for (auto c : text)
{
cout << c << endl;
}
return 0;
}

练习3.15:改写上题的程序,不过这次读入的是字符串。

#include <iostream>
#include <vector>
#include <string>//不要忘记加,输入时会出错

using namespace std;
int main()
{
string str;
vector<string> strVec;
char YN;
while (cin >> str)
{
strVec.push_back(str);
cout << "你是否要继续?" << endl;

cin >> YN;
if (YN != 'y' && YN != 'Y')
{
break;
}
}
for (auto c : strVec)
{
cout << c << endl;
}
return 0;
}

举个例子,假设有一组成绩的集合,其中成绩的取值是从0到100。以10分为一个分数段,要求统计各个分数段各有多少个成绩。

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<unsigned> scores(11, 0);
unsigned grade;
while (cin >> grade)
{
if (grade <= 100)
{
++scores[grade / 100];
}
if (grade == 111)
{
break;
}
}
int i = 0;
for (auto c : scores)
{
cout <<++i<<"阶段"<< c << endl;
}
return 0;
}

练习3.16:编写一段程序,把练习3.13中vector对象的容量和具体内容输出出来。检验你之前的回答是否正确,如果不对,回过头重新学习3.3.1节(第87页)直到弄明白错在何处为止。

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


int main()
{
    vector<int> v1;
    vector<int> v2(10);
    vector<int> v3(10, 42);
    vector<int> v4{10};
    vector<int> v5{10, 42};
    vector<string> v6{10};
    vector<string> v7{10, "hi"};


    cout << "v1的元素个数是:" << v1.size() << endl;
    if (v1.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v1的元素分别是:" << endl;
        for(auto e : v1)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    cout << "v2的元素个数是:" << v2.size() << endl;
    if (v2.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v2的元素分别是:" << endl;
        for(auto e : v2)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    cout << "v3的元素个数是:" << v3.size() << endl;
    if (v3.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v3的元素分别是:" << endl;
        for(auto e : v3)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    cout << "v4的元素个数是:" << v4.size() << endl;
    if (v4.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v4的元素分别是:" << endl;
        for(auto e : v4)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    cout << "v5的元素个数是:" << v5.size() << endl;
    if (v5.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v5的元素分别是:" << endl;
        for(auto e : v5)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    cout << "v6的元素个数是:" << v6.size() << endl;
    if (v6.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v6的元素分别是:" << endl;
        for(auto e : v6)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    cout << "v7的元素个数是:" << v7.size() << endl;
    if (v7.size() > 0)       //当vector含有元素时逐个输出
    {
        cout << "v7的元素分别是:" << endl;
        for(auto e : v7)    //使用范围for语句遍历每一个元素
        {
            cout << e << " ";
        }
        cout << endl;
    }


    return 0;
}

练习3.17:从cin读入一组词并把它们存入一个vector对象,然后设法把所有词都改写为大写形式。输出改变后的结果,没个词占一行。

int main()
{
vector<string> vString;//元素类型为string的vector对象
string s; //记录用户的输入值
char cont = 'y';//与用户交互,决定是否继续输入
cout << "请输入第一个词:" << endl;
while (cin >> s)
{
vString.push_back(s);//向vector对象中添加元素
cout << "您要继续吗(y or n)? " << endl;
cin >> cont;
if (cont != 'y' && cont != 'Y')
{
break;
}
cout << "请输入下一个词:" << endl;
}
cout << "转换后的结果是:" << endl;
for (auto &c : vString)
{
for (auto &str : c)
{
str = toupper(str);
}

cout << c;
}
return 0;
}

练习3.20:读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来。改写你的程序,这次要求先输出第1个和最后1个元素的和,接着输出第2个和倒数第2个元素的和,以此类推。

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


int main()
{
vector<int> vInt;
int iVal;
cout << "请输入一组数字:" << endl;
char YN = 'y';
while (cin >> iVal)
{
vInt.push_back(iVal);
cin >> YN;
if (YN != 'y'&&YN != 'Y' )
{
break;
}
}
if (vInt.size() == 0)
{
cout << "没有任何元素" << endl;
return -1;
}
cout << "相邻两项的和依次是:" << endl;
for (decltype(vInt.size()) i = 0; i < vInt.size() - 1; i += 2)
{
//求相邻两项的和
cout << vInt[i] + vInt[i + 1] << " ";
//每行输出5个数字
if ((i + 2) % 10 == 0)
{
cout << endl;
}
}
//如果元素数是奇数,单独处理最后一个元素
if (vInt.size() % 2 != 0)
{
cout << vInt[vInt.size() - 1];
}
return 0;
}

//这里用到了高中学的数列的知识,奇偶求中间值的下表

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


int main()
{
vector<int> vInt;
int iVal;
cout << "请输入一组数字:" << endl;
char YN = 'y';
while (cin >> iVal)
{
vInt.push_back(iVal);
cin >> YN;
if (YN != 'y'&&YN != 'Y' )
{
break;
}
}
if (vInt.size() == 0)
{
cout << "没有任何元素" << endl;
return -1;
}
cout << "相邻两项的和依次是:" << vInt.size() / 2<<endl;
for (decltype(vInt.size()) i = 0; i < vInt.size()/2; i++)
{
//求相邻两项的和
cout << vInt[i] + vInt[(vInt.size() - 1) - i] << " ";
//每行输出5个数字
if ((i + 2) % 10 == 0)
{
cout << endl;
}
}
//如果元素数是奇数,单独处理最后一个元素
if (vInt.size() % 2 != 0)
{
cout << vInt[vInt.size() / 2];
}
return 0;
}

练习3.21:请使用迭代器重做3.3.3节(第94页)的第一个练习。

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    vector<int> v1;
    vector<int> v2(10);
    vector<int> v3(10, 42);
    vector<int> v4{10};
    vector<int> v5{10, 42};
    vector<string> v6{10};
    vector<string> v7{10, "hi"};


    cout << "v1的元素个数是:" << v1.size() << endl;
    if (v1.cbegin() != v1.cend())       //当vector含有元素时逐个输出
    {
        cout << "v1的元素分别是:" << endl;
        for(auto it = v1.cbegin(); it != v1.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }


    cout << "v2的元素个数是:" << v2.size() << endl;
    if (v2.cbegin() != v2.cend())       //当vector含有元素时逐个输出
    {
        cout << "v2的元素分别是:" << endl;
        for(auto it = v2.cbegin(); it != v2.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "v3的元素个数是:" << v3.size() << endl;
    if (v3.cbegin() != v3.cend())       //当vector含有元素时逐个输出
    {
        cout << "v3的元素分别是:" << endl;
        for(auto it = v3.cbegin(); it != v3.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "v4的元素个数是:" << v4.size() << endl;
    if (v4.cbegin() != v4.cend())       //当vector含有元素时逐个输出
    {
        cout << "v4的元素分别是:" << endl;
        for(auto it = v4.cbegin(); it != v4.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "v5的元素个数是:" << v5.size() << endl;
    if (v5.cbegin() != v5.cend())       //当vector含有元素时逐个输出
    {
        cout << "v5的元素分别是:" << endl;
        for(auto it = v5.cbegin(); it != v5.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "v6的元素个数是:" << v6.size() << endl;
    if (v6.cbegin() != v6.cend())       //当vector含有元素时逐个输出
    {
        cout << "v6的元素分别是:" << endl;
        for(auto it = v6.cbegin(); it != v6.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << "v7的元素个数是:" << v7.size() << endl;
    if (v7.cbegin() != v7.cend())       //当vector含有元素时逐个输出
    {
        cout << "v7的元素分别是:" << endl;
        for(auto it = v7.cbegin(); it != v7.cend(); it++)//使用范围for语句遍历每一个元素
        {
            cout << *it << " ";
        }
        cout << endl;
    }

    return 0;
}

结果:

v1的元素个数是:0
v2的元素个数是:10
v2的元素分别是:
0 0 0 0 0 0 0 0 0 0
v3的元素个数是:10
v3的元素分别是:
42 42 42 42 42 42 42 42 42 42
v4的元素个数是:1
v4的元素分别是:
10
v5的元素个数是:2
v5的元素分别是:
10 42
v6的元素个数是:10
v6的元素分别是:


v7的元素个数是:10
v7的元素分别是:
hi hi hi hi hi hi hi hi hi hi

练习3.22:修改之前那个输出text第一段的程少挤,首先把text的第一段全都改成大写形式,然后再输出它。

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


int main()
{
vector<string> text;
string s;
char YN = 'y';
//利用getline读取一句话,直接回车产生一个空串,表示段落结束
while (getline(cin, s))
{
text.push_back(s);  //逐个添加到text中
cout << "是否继续输入:是('y'or'Y'),否(任意)";
cin >> YN;
if (YN != 'y'&&YN != 'Y')
{
break;
}
}
//利用迭代器遍历全部字符串,遇空串停止循环
for (auto it = text.begin(); it != text.end() && !it->empty(); it++)
{
//利用迭代器遍历当前字符串
for (auto it2 = it->begin(); it2 != it->end(); it2++)
{
*it2 = toupper(*it2);   //利用toupper改写成大写形式
}
cout << *it << endl;        //输出当前字符串
}


return 0;
}

练习3.23:编写一段程序,创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出vector对象的内容,检验程序是否正确。

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


int main()
{
vector<int> intvec;
for (int i = 0; i < 10;i++)
{
int c;
cin >> c;
intvec.push_back(c);
}
for (auto it = intvec.begin(); it != intvec.end(); ++it)
{
cout << *it<<" ";
}
cout << endl;
for (auto it = intvec.begin(); it != intvec.end(); it++)
{
*it *= 2;
cout << *it << " ";        //输出当前数字
}

system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值