标题:vector<struct> student 如何给成员传值
只看楼主
sudong0913
Rank: 1
等 级:新手上路
帖 子:38
专家分:5
注 册:2009-4-15
结帖率:76.92%
已结贴  问题点数:5 回复次数:4 
vector<struct> student 如何给成员传值
#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;
    .................
    //如何给结构体成员传值!!
   //求高手指教~~~

}
搜索更多相关主题的帖子: vector student struct 传值 成员 
2010-09-02 18:30
luzhiwei512
Rank: 2
等 级:论坛游民
帖 子:4
专家分:21
注 册:2007-8-9
得分:2 

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

2010-09-02 19:23
alterhz
Rank: 1
等 级:新手上路
帖 子:1
专家分:3
注 册:2010-9-2
得分:3 

#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;
    }

}
2010-09-03 00:01
ycsky85
Rank: 1
等 级:新手上路
帖 子:4
专家分:3
注 册:2010-8-17
得分:0 
顶楼上~
2010-09-03 17:20
melochale
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-11-2
得分:0 
很简单嘛,就是一个struct类型的vector对象,用push_back添加。
2010-09-10 06:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-317541-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.292835 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved