首页 > 编程语言 >C++ STL的简单应用(vector容器专题)

C++ STL的简单应用(vector容器专题)

时间:2023-01-12 00:22:24浏览次数:54  
标签:cout STL back C++ v1 vector push include name

#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
//#include <algorithm>

using namespace std;
//vector容器的简单应用
void demo1() {
    vector<int> v1;
    v1.push_back(4);
    v1.push_back(2);
    v1.push_back(4);
    v1.push_back(3);
    v1.push_back(4);

    cout << "v1中的元素个数:" << v1.size() << endl;
    cout << "v1中保存的元素:" << endl;
    //第一种方式,数组访问
    //for (int i = 0; i < v1.size(); i++) {
    //    cout << v1[i] << endl;
    //}
    //第二种方式,迭代器访问
    vector<int>::iterator is = v1.begin();
    for (; is != v1.end(); is++) {
        cout << *is << endl;
    }
    //统计容器中某个元素的个数
    int rcount = count(v1.begin(), v1.end(), 4);
    cout << "v1中数值为4的元素一共有" << rcount << "个" << endl;
}
class Student {
public:
    Student(int age, const char* name) {
        this->age = age;
        strncpy_s(this->name, name, 64);
        cout << "调用了构造函数" << endl;
    }
    Student(const Student& s) {
        this->age = s.age;
        strncpy_s(this->name, s.name, 64);
        cout << "调用了拷贝构造函数" << endl;
    }
    ~Student() {
        cout << "调用了析构函数" << endl;
    }
    int age;
    char name[64];
};
void demo2() {
    vector<Student *> s; //使用指针提高效率,此处不可用引用!
    Student s1(12,"小王");
    Student s2(23,"大王");
    s.push_back(&s1);
    s.push_back(&s2);
    //第一种方式,数组访问
    /*for (int i = 0; i < s.size(); i++) {
        cout << (*s[i]).age<<":"<<(*s[i]).name << endl;
    }*/
    //第二种方式,迭代器访问
    vector<Student *>::iterator it = s.begin();
    for (; it != s.end(); it++) {
        cout << (**it).name<<":"<<(**it).age << endl;
    }
}
int main() {
    demo1();
    cout << endl;
    demo2();
    
    system("pause");
    return 0;
}

标签:cout,STL,back,C++,v1,vector,push,include,name
From: https://www.cnblogs.com/smartlearn/p/17045255.html

相关文章

  • c++ unit test via gtest
    //model/book.h#pragmaonce#include<iostream>usingnamespacestd;classbook{public:book()=default;~book()=default;book(constbook&......
  • C++ 使用 new 创建二维数组
    1.直接创建C++使用new创建二维数组最直接的方法就是newT[M][N]。返回的指针类型是T(*)[N],它是指向数组的指针,可以直接使用数组下标形式访问元素。释放内存直接使......
  • 【教你学Qt桌面端开发】pt1:浅谈Qt:特色C++主义类库
    还在为头脑简单看不懂代码而发愁吗?还在为思想浅薄只会人云亦云、拾人牙慧、鹦鹉学舌而遭人鄙夷吗?《教你写代码》,从另一维度解读代码,让你成为见解独特的黑马观众。教你学Q......
  • C++ 扩展的显示类型转换
      ......
  • C++实现顺序队列(循环队列)相关操作代码
    #include<iostream>#include<cstdlib>usingnamespacestd;#defineMAXSIZE100#defineOK1#defineERROR0#defineOVERFLOW-1typedefintStatus;typedefintElemtype......
  • Visual Studio Code – C++ 入门
    ——基于VisualStudioCode官方文档的全面的、具体的入门级教程作者:XiXu在本教程中,您将为使用UCRT64中的GCCC++编译器(g++)和GDB调试器配置VisualStudio......
  • c++静态库和动态库的添加
    #声明要求的cmake最低版本cmake_minimum_required(VERSION2.8)#声明一个cmake工程project(helloSLAM)#设置编译模式set(CMAKE_BUILD_TYPE"Debug")#共享库add_l......
  • C++用递归实现求解相关函数
    //递归实现Hanoi塔问题#include<iostream>#include<cstdlib>usingnamespacestd;#defineMAXSIZE100#defineOK1#defineERROR0typedefintStatus;typedefintElemty......
  • C++实现链栈相关操作代码
    #include<iostream>#include<cstdlib>usingnamespacestd;#defineMAXSIZE100#defineOK1#defineERROR0typedefintStatus;typedefintElemtype;typedefstructSta......
  • STL关联式容器使用注意、概念总结
    引入继上文STL序列式容器使用注意、概念总结继续总结关联式容器的概念以及一些使用事项。关联式容器与容器适配器基础容器STL中的关联式底层容器:RBtree,hashtabl......