c++_vector_初探1

本文介绍了如何使用C++标准库中的vector容器进行多种数据处理任务,包括整数与字符串的输入、大写转换及特定数学运算等。
ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

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

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> v;
    int x=0;
    while(cin>>x){
        v.push_back(x);
        char y='y';

        cout<<"要继续吗?输入n退出,输入其他字符继续。"<<endl;
        cin>>y;
        if(y=='n')
            break;
    }

    for(auto c:v)
        cout<<c<<endl;
    return 0;
}

以上的程序中,单个输入整型数据,通过循环中的判断来控制输入。

  1. 改写第一题,这次读入字符串。
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> v;
    string x="";
    while(cin>>x){
        v.push_back(x);
        char y='y';

        cout<<"要继续吗?输入n退出,输入其他字符继续。"<<endl;
        cin>>y;
        if(y=='n')
            break;
    }

    for(auto c:v)
        cout<<c<<endl;
    return 0;
}

将容器改为字符串容器即可。

输出的结果

what
要继续吗?输入n退出,输入其他字符继续。
y
is
要继续吗?输入n退出,输入其他字符继续。
y
your
要继续吗?输入n退出,输入其他字符继续。
y
name
要继续吗?输入n退出,输入其他字符继续。
n
what
is
your
name
  1. 读入一组词,并将所有词改成大写形式。
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<string> v;
    string x="";
    while(cin>>x){
        v.push_back(x);
        char y='y';

        cout<<"要继续吗?输入n退出,输入其他字符继续。"<<endl;
        cin>>y;
        if(y=='n')
            break;
    }

    for(auto &c:v){
        for(auto &s:c)
            s=toupper(s);
        cout<<c<<endl;
    }
    return 0;
}

这里面有对大写函数toupper(char ch)函数的应用,函数参数应为char类型,不能直接将string类型的数据传入,应当把每个string再拆分成char之后再使用toupper函数。

what
要继续吗?输入n退出,输入其他字符继续。
y
is
要继续吗?输入n退出,输入其他字符继续。
y
your
要继续吗?输入n退出,输入其他字符继续。
y
name
要继续吗?输入n退出,输入其他字符继续。

n
WHAT
IS
YOUR
NAME

--------------------------------
Process exited with return value 0
Press any key to continue . . .
  1. 读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出。
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> v;
    int x=0;
    while(cin>>x){
        v.push_back(x);
    }

    for(decltype(v.size()) i=0;i<v.size()-1;i+=2){

        cout<<v[i]+v[i+1]<<" ";

    } 
    if(v.size()%2!=0)
        cout<<v[v.size()-1];

    return 0;
}

采用下标访问vector元素也很方便。这里的“^Z”表示按下了Ctrl+Z来终止输入。

测试结果1:
2 6 5 8 2 3
^Z
8 13 5
--------------------------------
Process exited with return value 0
Press any key to continue . . .

测试结果2:
7 6 4 4 5^Z
13 8 5
--------------------------------
Process exited with return value 0
Press any key to continue . . .

改编:将首尾元素相加的值输出。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> v;
    int x=0;
    while(cin>>x){
        v.push_back(x);
    }

    for(decltype(v.size()) i=0;i<v.size()/2;i++){

        cout<<(v[i]+v[v.size()-1-i])<<" ";

    } 
    if(v.size()%2!=0)
        cout<<v[v.size()/2];

    return 0;
}

结果:

1. 输入的数字个数为奇数。
4 7 3 9 2^Z
6 16 3
--------------------------------
Process exited with return value 0
Press any key to continue . . .

2. 输入的数字个数为偶数。
2 3 9 3 2 1
^Z
3 5 12
--------------------------------
Process exited with return value 0
Press any key to continue . . .

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值