C++容器
基本语法:
vector <type> variable (elements)
For example:
vector <int> rooms (9);
Let's break it down:
type
defines a data type stored in a vector (e.g., <int>, <double> or <string>)variable
is a name that you choose for the dataelements
specified the number of elements for the data
It is mandatory to determine the type and variable name. However, the number of elements is optional.
Basically, all the data elements are stored in contiguous storage. Whenever you want to access or move through the data, you can use iterators.
The data elements in C++ vectors are inserted at the end. Use modifiers to insert new elements or delete existing ones.
Theory is great, but we recommend digging deeper!
C++ for Beginners: The Ultimate Bootcamp
Iterators
An iterator allows you to access the data elements stored within the C++ vector. It is an object that functions as a pointer. There are five types of iterators in C++: input, output, forward, bidirectional, and random access.
C++ vectors support random access iterators. Here are a few function you may use with iterators for C++ vectors:
vector::begin()
returns an iterator to point at the first element of a C++ vector.vector::end()
returns an iterator to point at past-the-end element of a C++ vector.vector::cbegin()
is similar tovector::begin()
, but without the ability to modify the content.vector::cend()
issimilar tovector::end()
but can’t modify the content.
Modifiers
As its name suggests, you can use a modifier to change the meaning of a specified type of data. Here are some modifiers you can use in C++ vectors:
vector::push_back()
pushes elements from the back.vector::insert()
inserts new elements to a specified location.vector::pop_back()
removes elements from the back.vector::erase()
removes a range of elements from a specified location.vector::clear()
removes all elements.
Breaking It Down With Examples
There are many ways to initialize C++ vectors. You can use them depending on your preferences or the size of your data.
Start with default value
#include <iostream>
#include <string>
#include <vector>
int main() {
// Vector with 5 integers
// Default value of integers will be 0.
std::vector < int >
vecOfInts(5);
for (int x: vecOfInts)
std::cout << x << std::endl;
}
Start with an array
#include <iostream>
#include <string>
#include <vector>
int main() {
// Array of string objects
std::string arr[] = {
"first",
"sec",
"third",
"fourth"
};
// Vector with a string array
std::vector < std::string > vecOfStr(arr,
arr +
sizeof(arr) / sizeof(std::string));
for (std::string str: vecOfStr)
std::cout << str << std::endl;
}
Start with a list
#include <iostream>
#include <string>
#include <vector>
#include <list>
int main() {
// std::list of 5 string objects
std::list < std::string > listOfStr;
listOfStr.push_back("first");
listOfStr.push_back("sec");
listOfStr.push_back("third");
listOfStr.push_back("fouth");
// Vector with std::list
std::vector < std::string > vecOfStr(listOfStr.begin(), listOfStr.end());
for (std::string str: vecOfStr)
std::cout << str << std::endl;
}
Start by copying from another vector
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector < std::string > vecOfStr;
vecOfStr.push_back("first");
vecOfStr.push_back("sec");
vecOfStr.push_back("third");
// Vector with other string object
std::vector < std::string > vecOfStr3(vecOfStr);
}
When using certain initialization, you might need to set the size of the vector. Size refers to the number of elements a vector contains. It is not the same as capacity, which is the maximum number of elements a vector can contain.
To manage it, you can use size()
function:
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector <int> v {
1,
2,
3,
4,
5
};
int n = v.size();
cout << "Size of the vector is :" << n;
}
You can also use the max_size()
function like this:
#include <iostream>
#include<vector>
using namespace std;
int main() {
vector <int> v {
1,
2,
3,
4,
5
};
std::cout << v.max_size() << std::endl;
}
Most of the time, you will need to access a specified element in a C++ vector. To do that, you can use the [] selector
function as shown below:
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector <int> v {
1,
2,
3,
4,
5
};
for (int i = 0; i < v.size(); i++)
cout << v.operator[](i) << " ";
}
If you want to replace a certain value with a new one, you can use = operator
:
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector <char> v {
'C',
'#'
};
vector <char> v1;
v1.operator = (v);
for (int i = 0; i < v.size(); i++)
std::cout << v[i];
}
C++ Vector: Useful Tips
- It is recommended to use C++ vector if your data elements are not predetermined.
- As a template class, C++ vectors offer better efficiency and reusability.
- Compared to arrays, there are more ways to copy vectors in C++.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2019-10-10 变异系数