注册 登录
编程论坛 C++教室

vector<struct> student 如何给成员传值

sudong0913 发布于 2010-09-02 18:30, 2052 次点击
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
struct student
{
    string name;
    double midterm;
    double final;
    vector<double> homework;
};
void main()
{
    vector<student> vec;
    .................
    //如何给结构体成员传值!!
   //求高手指教~~~

}
4 回复
#2
luzhiwei5122010-09-02 19:23

1,student a;
a.name=""
vector<student> vec;
vec.push_back(a);
2,vector<student>::iterator it;
it->name=""

#3
alterhz2010-09-03 00:01

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

struct student
{
    string name;
    double midterm;
    double final;
    vector<double> homework;
};

void main()
{
    vector<student> vec;

    student stu1;
    stu1.name = "aaa";
    stu1.midterm = 1.12;
    stu1.final = 3.33;

    stu1.homework.push_back((double)3.3);
    stu1.homework.push_back((double)3.5);
    stu1.homework.push_back((double)3.4);

    //将stu1加入vector
    vec.push_back(stu1);

    student stu2;
    stu2.name = "bbb";
    stu2.midterm = 5.62;
    stu2.final = 9.63;

    stu2.homework.push_back((double)4.9);
    stu2.homework.push_back((double)5.3);

    //将stu1加入vector
    vec.push_back(stu2);

//输出
    vector<student>::iterator it = vec.begin();

    for (; it != vec.end(); ++it)
    {
        cout << it->name<< " " << it->midterm << " " << it->final << endl;

        vector<double>::iterator itD = it->homework.begin();

        for (; itD != it->homework.end(); ++itD)
        {
            cout << *itD << " ";
        }

        cout << endl << "====================================================" << endl;
    }

}
#4
ycsky852010-09-03 17:20
顶楼上~
#5
melochale2010-09-10 06:02
很简单嘛,就是一个struct类型的vector对象,用push_back添加。
1