結構體容器vector

2021-07-11 11:47:15 字數 942 閱讀 8466

最近在做處理資料的問題,本來想建立乙個結構體,裡面存放兩個陣列,在乙個main函式裡進行了設計,也能成功執行,但是寫成函式呼叫的形式就會出錯,並且這種寫法必須設定陣列的大小。

#include

#include

using

namespace

std;

int main()

stu;

for(int i = 0; i < 3; i++)

//printstruct(stu);

for(int i = 0; i < 3; i++)

但是可以通過容器對該方法進行改進,即容器的型別是結構體,通過對結構體操作,然後把把結構體push_back到容器裡即可,非常方便,具體**如下:

cstrupama.h:

#pragma once

#include

#include

#include

struct students

;class csetstruct

;

cstrupama.cpp:

#include "cstrupara.h"

using

namespace

std;

void csetstruct::outstrucval(std::vector

stu)

outstrucval(vecstu);

}

mainfun.cpp:

#include

#include

#include "cstrupara.h"

using

namespace

std;

int main()

Vector容器中存放結構體型別的變數

如果要在vector容器中存放結構體型別的變數,經常見到兩種存放方式.方式一 放入這個結構體型別變數的副本。方式二 放入指向這個結構體型別變數的指標。假設結構體型別變數是這樣的,cpp view plain copy print?typedef struct student studentinfo ...

vector容器中存放結構體型別的變數

如果要在vector容器中存放結構體型別的變數,經常見到兩種存放方式.方式一 放入這個結構體型別變數的副本。方式二 放入指向這個結構體型別變數的指標。假設結構體型別變數是這樣的,cpp view plain copy print?typedef struct student studentinfo ...

結構體vector使用總結

主要有以下幾種方法 cpp view plain copy vector list list.push back 1 list.push back 2 一 初始化構造時拷貝 cpp view plain copy vector tem list 這種拷貝,相當於複製了乙份資料,list中的資料不變。...