MS C++ 2010 supports some intermediate C++ between
C++ 2003 and
C++ 2011. For example it supports lambda expressions.
Howeve
MS VC++ 2010 does not support initializer lists. Thus you may not for example write
std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
However the similar result can be obtained in
MS VC++ 2010 using standard algorithm
std::iota declared in header
<numeric>:
#include <numeric>
//...
std::vector<int> v( 10 );
std::iota( v.begin(), v.end(), 0 );
If you would use a compiler that supports
C++ 2011 then you could initialize a "multidimensional" vector for example the following way using initializer lists.
#include <iostream>
#include <vector>
int main()
{
std::vector<std::vector<std::vector<int>>> v =
{
{
{ 1, 2, 3, },
{ 4, 5, 6, }
},
{
{ 7, 8, 9, },
{ 0, 1, 2, }
}
};
for ( const auto &v1 : v )
{
for ( const auto &v2 : v1 )
{
for ( int x : v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
You can not do the same using
MS VC++ 2010. You should use some approach with loops and/or algorithms. For example
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::vector<std::vector<int>>> v( 2 );
for ( std::vector<std::vector<std::vector<int>>>::size_type i = 0; i < v.size(); i++ )
{
const size_t N = 2;
const size_t M = 3;
struct value
{
int a[N][M];
} init;
if ( i == 0 )
{
value tmp =
{
{
{ 1, 2, 3, },
{ 4, 5, 6, }
}
};
init = tmp;
}
else
{
value tmp =
{
{
{ 7, 8, 9, },
{ 0, 1, 2, }
}
};
init = tmp;
}
std::transform( std::begin( init.a ), std::end( init.a ), std::back_inserter( v ),
[]( const int ( &row )[M] )
{
return std::vector<int>( std::begin( row ), std::end( row ) );
} );
}
for each ( const auto &v1 in v )
{
for each ( const auto &v2 in v1 )
{
for each ( int x in v2 ) std::cout << x << ' ';
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
The output is
1 2 3
4 5 6
7 8 9
0 1 2