#include<stdio.h>
printf("%f\n",vf[i]);
#include<vector>
#include<stdio.h>
//TODO: Use the standard namespace
using namespace std;
int main() {
//declare and define a vector with values
// {5.0, 5.0, 5.0} and print
// the vector to the terminal using cout
// Hint: the syntax vector<datatype> varname(length, value) might help
vector<float> vf(4,5.0);
// Use push back to add the values 3.0, 2.5, 1.4
// to the back of the vector
vf.push_back(3.0);
vf.push_back(2.5);
vf.push_back(1.4);
// Part 3: Print out the vector again using cout
for (int i = 0;i<vf.size();i++)
{
//cout<<vf[i]<<endl;
printf("%f\n",vf[i]);
}
//Use the vector assign method to override the current vector.
// The overriden vector should have 3 elements with
// the values {5.0, 5.0, 5.0}
vector<float> vf3(3,5.0);
printf("%s\n","-----------------------------------");
vf.assign(vf3.begin(),vf3.begin()+3);
int size2 = vf.size();
for(int i=0;i<size2;i++)
{
// cout<<vf[i]<<endl;
printf("%f\n",vf[i]);
}
return 0;
}
本文通过C++代码示例,详细介绍了如何使用std::vector容器进行初始化、元素添加、打印及赋值等操作,是理解C++标准模板库(STL)中vector用法的良好教程。

被折叠的 条评论
为什么被折叠?



