How to split a vector into three variables in C++

In Python the following expression splits the list a into the three variables x,y,z:

a = [2,5,7]
(x,y,z) = a

Is there anything similar in C++? I’ve tried writing the following code but it does not works:

#include <iostream>

int main() {
   int a[] = {3, 5, 7};
   int x,y,z;

   (x,y,z) = a;
}

Compiler fails with the following error:

error: assigning to 'int' from incompatible type 'int [3]'
    (x, y, z) = arr;

Answer

You can use structured bindings starting from C++17, like so:

std::tuple<int, int, int> a{ 1, 2, 3  };

auto [x, y, z] = a;

See: https://en.cppreference.com/w/cpp/language/structured_binding

Pre-C++17 you can use std::tie, like so:

std::tuple<int, int, int> a{ 1, 2, 3  };

int x, y, z;
std::tie(x, y, z) = a;

See https://en.cppreference.com/w/cpp/utility/tuple/tie

If it has to be something other than std::tuple or std::pair, you’ll have to assign the variables manually:

x = a[0]; y = a[1]; x = a[2];