vector<float>如何实现元素存入和读取
时间: 2024-02-25 13:54:13 浏览: 38
泛型类vector<T>源码分析
vector<float>是一个动态数组,可以使用push_back()函数将元素存入vector中,使用下标运算符[]或者at()函数读取元素。例如,下面的代码演示了如何存入和读取vector<float>中的元素:
```
#include <iostream>
#include <vector>
int main() {
std::vector<float> vec;
vec.push_back(1.0);
vec.push_back(2.0);
vec.push_back(3.0);
std::cout << "The elements in the vector are: ";
for (int i = 0; i < vec.size(); i++) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
std::cout << "The second element in the vector is: " << vec.at(1) << std::endl;
return 0;
}
```
输出结果为:
```
The elements in the vector are: 1 2 3
The second element in the vector is: 2
```
阅读全文