[英]Multi-dimensional Vector Pointer
我有以下多維向量
int main()
{
vector< vector<string> > tempVec;
someFunction(&tempVec);
}
void someFunction(vector< vector<string> > *temp)
{
//this does not work
temp[0]->push_back("hello");
}
當我有向量指針時,如何將數據推入向量? 以下代碼不起作用。
temp[0]->push_back("hello");
你需要
(*temp)[0].push_back("hello")
那是:
temp
以獲得vector<vector<string> > &
vector<string> &
.
而不是->
因為您不再處理指針 也就是說,如果someFunction
使用vector< vector<string> >&
而不是指針: temp[0].push_back("hello")
會更容易。 引用不允許使用指針算術或null指針,因此它們使操作更難,並且更能暗示所需的實際輸入類型(單個vector
,而不是它們的可選vector
或數組)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.