Mar*_*les -2 c++ pointers vector
我有两个常量向量:
const vector<int> A = { 0, 12, 23, 34, 45, 56, 67, 78, 89 };
const vector<int> B = { 13, 24, 35, 46, 57, 68, 79 };
Run Code Online (Sandbox Code Playgroud)
我想像这样在他们之间切换
int main()
{
vector<int> myVector;
if ( something happens )
point myVector to A;
else
point myVector to B;
while ( a condition )
{
do something with ( myVector );
}
}
Run Code Online (Sandbox Code Playgroud)
我如何指向矢量A或B我应该如何声明myVector?
你可以使用一个参考:
auto& myVector = somethingHappens ? A : B;
while ( a condition ) {
do something with ( myVector );
}
Run Code Online (Sandbox Code Playgroud)
现在myVector是一个"新名"无论是A或B,根据您的病情.
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |