免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7480 | 回复: 3
打印 上一主题 下一主题

[C++] vector<struct>()的问题 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2013-05-07 17:28 |显示全部楼层
vector <struct Fact_t>g_FactDB(FACTNUM);
为什么要加 struct 呢?

论坛徽章:
0
2 [报告]
发表于 2013-05-07 17:34 |显示全部楼层
那上面 不说了

但在c++里很简单,直接
        struct Student
        {
           int a;
        };
于是就定义了结构体类型Student,声明变量时直接Student stu2;

论坛徽章:
0
3 [报告]
发表于 2013-05-07 17:56 |显示全部楼层
不知道你是不是这个意思? 随便写了个.
Test.h

  1. #ifndef TestStruct_Test_h
  2. #define TestStruct_Test_h

  3. #include <vector>

  4. using namespace std;

  5. struct Fact_t{
  6.     int a;
  7.     int b;
  8. };

  9. #endif
复制代码
main.cpp
  1. #include <iostream>
  2. #include <iterator>
  3. #include <stdio.h>
  4. #include "Test.h"

  5. int main(int argc, const char * argv[])
  6. {
  7.     Fact_t a1;
  8.     a1.a = 5;
  9.     a1.b = 6;
  10.    
  11.     Fact_t b1;
  12.     b1.a = 15;
  13.     b1.b = 16;
  14.    
  15.     Fact_t c1;
  16.     c1.a = 25;
  17.     c1.b = 26;
  18.     vector<Fact_t> FactV;
  19.     FactV.push_back(a1);
  20.     FactV.push_back(b1);
  21.     FactV.push_back(c1);
  22.    
  23.     vector<Fact_t>::iterator it;
  24.     for (it = FactV.begin(); it != FactV.end(); ++it) {

  25.         printf("a: %d\n", it->a);
  26.         printf("b: %d\n", it->b);
  27.     }
  28.     // insert code here...
  29.     std::cout << "Hello, World!\n";
  30.     return 0;
  31. }
复制代码
输出
a: 5
b: 6
a: 15
b: 16
a: 25
b: 26

论坛徽章:
0
4 [报告]
发表于 2013-05-07 18:01 |显示全部楼层
vector 本身就是容器, 你加20是想干啥?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP