C++学习第二天

vector

vector介绍

向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器。与string相同, vector 同属于STL(Standard Template Library, 标准模板库)中的一种自定义的数据类型, 可以广义上认为是数组的增强版;
  在使用它时,需要包含vector头文件,#include< vector >;
  vector 容器与数组相比其优点在于它能够根据需要随时自动调整自身的大小以便容下所要放入的元素。此外, vector 也提供了许多的方法来对自身进行操作。

vector初始化

std::vector<std::string> text; //空对象,动态分配空间,效率较差,每次分配的空间为2^n
std::vector<std::string> text(10); //包含10个元素的vector对象
std::vector<std::string> text(10, "text"); //包含10个"text"的vector对象
std::vector<std::string> text{10, "text"}; //包含10个"text"的vector对象

vector练习

#include <iostream>
#include <vector>

int main() {
    std::vector<std::string> text;
    std::string word;
    while(std::cin >> word) {
        text.push_back(word);
    }
    int L_text = text.size();
    for (int index = 0; index < L_text; index++) {
        for (auto &c : text[index]) {
            c = std::toupper(c);
        }
    }
    for (int index = 0; index < L_text; index++) {
        std::cout << text[index] << std::endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值