[英]passing an array of structs to a function in c++
我已經編寫了一個運行正常的程序。 我需要把它分解成函數。 我有 3 個結構數組。 我想做一個從文件中讀取信息並將其回顯出來的函數。 我只需要一個關於如何通過它的例子。 我會發布我的代碼,但我不希望其他學生接受它。 謝謝。
如果您使用 C 數組:
struct A { int v; }
A data[10];
void func(A *array, size_t n) {
}
func(data, 10);
或者,如果您使用的是向量:
std::vector<A> vec;
void func(std::vector<A>& array) {
}
func(vec);
由於您將其標記為“C++”,因此我假設您使用的是向量(:-))
void f1 ( yourVector& yourVector )
{
// do something with the vector as read-write (for example fill the vector with somthing).
}
void f2 ( const yourVector& yourVector )
{
// do something with the vector as read-only.
}
int main()
{
std::vector <yourStruct> yourVector;
f1( yourVector );
f2( yourVector );
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.