35. c++动态数组(std::vector)

vector在c++的标准模版库STL中,STL中包含了很多容器的数据结构,后面会详细描述。vector(向量)数组大小是动态变化的。一般情况下元素不为指针时,它的内存是连续的。

35.1  vector的简单使用


#include <iostream>
#include <vector>
struct Vertex {
    float x,y,z;
};
std::ostream& operator<<(std::ostream& stream,const Vertex v){
    stream<<v.x<<","<<v.y<<","<<v.z;
    return stream;
}
void print(const Vertex& v){ //记得使用引用传递
   std::cout<<v<<std::endl;
    
}
int main(int argc, const char * argv[]) {
    // insert code here...
    std::vector<Vertex> vertexs;
    vertexs.push_back({1,2,3});//按成员声明的顺序使用列表构造
    vertexs.push_back({4,5,6});
    for (int i=0; i<vertexs.size(); ++i) {
        std::cout<<vertexs[i]<<std::endl;
    }
    vertexs.erase(vertexs.begin()+1);
    std::cout<<"after erase:"<<std::endl;
    for(Vertex& v:vertexs){ //使用引用 避免产生拷贝
        std::cout<<v<<std::endl;
    }
    std::co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值