现在的位置: 首页 > 综合 > 正文

Vector 定义struct

2012年07月20日 ⁄ 综合 ⁄ 共 686字 ⁄ 字号 评论关闭

摘自:http://stackoverflow.com/questions/8067338/c-vector-of-structs-initialization

 

#include <vector>
using namespace std;
typedef struct
{
    CString name;    
    int marks; 
    int credits;
}subject;

int _tmain(int argc, _TCHAR* argv[])
{
   vector<subject> sub;
   //Push back new subject created with default constructor. 
   sub.push_back(subject()); 

   //Vector now has 1 element @ index 0, so modify it. 
   sub[0].name = "english";

   //Add a new element if you want another: 
   sub.push_back(subject()); 

   //Modify its name and marks. 
   sub[1].name = "math"
   sub[1].marks = 90;
   int iCount=sub.size();
   for(int i=0;i<iCount;i++)
   {

       CString name0=sub[i].name;
   }
    return 0;
}

 

抱歉!评论已关闭.