C++ STL之向量vector

/*vector_example.cpp*/
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> msg = {"Hello", "C++", "World", "from", "VSCode", "and the C++ extension!"};

    for (const string& word : msg) {
        cout << word << " ";
    }
    cout << endl;

    return 0;
}
  • vector.size() 返回向量中元素个数

  • vector.data()返回指向存储数组的地址(类似于数组名为指针)

  • vector的assign()方法,会清除掉vector容器中以前的内容:

    • void assign(const_iterator first,const_iterator last);将区间[first,last)的元素赋值到当前的vector容器中
    • void assign(size_type n,const T& x = T());赋n个值为x的元素到vector容器中
  • C++打印vector数据:

posted @ 2023-09-19 22:20  达可奈特  阅读(19)  评论(0编辑  收藏  举报