vector<int *>和vector<struct *> 题目是这个,居然作为标题发布不了,提示我输入有误!!!!
//前言:在看VINS源码的时候发现的, 源码里面是这样的std::vector<ResidualBlockInfo *> factors;
//这里的ResidualBlockInfo是这样定义的struct ResidualBlockInfo,也就是结构体。为了便于自己理解和实现,
//我先从vector<int *>开始搞清楚。
1 vector<int *>是什么
指向vector<int>的指针
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int *> test;
int array[] = { 1,2,3,4,5 };
cout << "nTemp value : ";
for (int i = 0; i< 5; i++)
{
int* nTemp = new int;
nTemp = &array[i];
cout << " " << nTemp;
test.push_back(nTemp);
}
cout << "\n";
// for (auto it = test.begin(); it != test.end(); it++)
cout << "the operate #it:test#: ";
for (auto it:test)
{
cout << " " << it; //修改为cout << " " << *it;
}
cout << "\n";
cout << "*it value : ";
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
cout << "\n";
system("pause");
return 0;
}
nTemp value : 0039FB7C 0039FB80 0039FB84 0039FB88 0039FB8C
the operate #it:test#: 0039FB7C 0039FB80 0039FB84 0039FB88 0039FB8C
*it value : 0039FB7C 0039FB80 0039FB84 0039FB88 0039FB8C
请按任意键继续. . .
for (auto it:test)
{
cout << " " << it;
}
for (auto it = test.begin(); it != test.end(); it++)
{
cout << " " << *it;
}
两者输出结果一样,都是数组元素的地址
再将代码中
cout << " " << it; //修改为cout << " " << *it;
输出结果是这样
nTemp value : 0038F894 0038F898 0038F89C 0038F8A0 0038F8A4
the operate #it:test#: 1 2 3 4 5
*it value : 0038F894 0038F898 0038F89C 0038F8A0 0038F8A4
请按任意键继续. . .
2 vector<struct *>
再来看在vector里面嵌套结构体struct * ,也就是 vector<struct *>
#include <iostream>
#include <vector>
#include<string>
#include<iomanip>
using namespace std;
struct Person {
string name;
int age;
double salary;
double sum_salary(double n) { double sum = 0.0; sum = 12 * n; return sum; }
};
int main()
{
Person z_san, l_si;
z_san.name = "z_san";
z_san.age = 20;
z_san.salary = 2345.45;
z_san.sum_salary(z_san.salary);
l_si.name = "l_si";
l_si.age = 23;
l_si.salary = 2222.22;
l_si.sum_salary(l_si.salary);
vector<Person *> test;
test.push_back(&z_san);
test.push_back(&l_si);
for (auto it : test)
{
cout.setf(ios::showpoint);//小数部分末尾为0时,是输不出来的!要想输出来,就得用showpoint了
// cout << it << endl;
// cout << it->sum_salary(l_si.salary) << endl;;
cout << "name : " << it ->name << endl;
cout << "age : " << it->age << endl;
cout << "salary : " << it->salary << endl;
cout << "sum_salary : " << setprecision(7) << it->sum_salary(it->salary) << endl;//头文件是#include<iomanip>
cout << "\n";
}
cout << "\n";
system("pause");
return 0;
}
name : z_san
age : 20
salary : 2345.45
sum_salary : 28145.40
name : l_si
age : 23
salary : 2222.220
sum_salary : 26666.64
请按任意键继续. . .
小插曲:在输出的时候,输出精度有问题(少输出一位小数),于是就网上查资料。找到了setprecision(int n)这个输出控制函数。头文件是#include<iomanip>。要想输出小数末尾部分的0,就得用cout.setf(ios::showpoint);
关于setprecision(int n) 我们用简单的例子来说明吧
int main()
{
double a = 9.8765432101230;
float b = 1.2345678;
double c = 234.4500;
cout.setf(ios::showpoint);
cout << "a : " << setprecision(1) << a << endl;
cout << "a : " << setprecision(4) << a << endl;
cout << "interrupted (b5 value): " << b << endl;//输出b,b的格式和上一句相同:setprecision(5)
cout << "a : " << setprecision(9) << a << endl;
cout << "a : " << setprecision(13) << a << endl;
cout << "a : " << setprecision(15) << a << endl;
cout << "interrupted (c15 value): " << c << endl;//输出c,c的格式:setprecision(15)
cout << "c : " << setprecision(1) << c << endl;
cout << "c : " << setprecision(4) << c << endl;
cout << "c : " << setprecision(6) << c << endl;
cout << "c : " << setprecision(8) << c << endl;
cout << "c : " << setprecision(10) << c << endl;
system("pause");
return 0;
}
/*
a : 1.e+01
a : 9.877
interrupted (b5 value): 1.235
a : 9.87654321
a : 9.876543210123
a : 9.87654321012300
interrupted (c15 value): 234.450000000000
c : 2.e+02
c : 234.4
c : 234.450
c : 234.45000
c : 234.4500000
请按任意键继续. . .
*/