Форум » C/C++ для начинающих (C/C++ for beginners) » undo function » Ответить

undo function

ramees: hi how can i initialize 3d vector in c++98 i am using visual studio 2010 i am wondering how undo is working in software's how can i code a basic undo function in c++

Ответов - 2

Сыроежка: 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 [pre2] std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; [/pre2] However the similar result can be obtained in MS VC++ 2010 using standard algorithm std::iota declared in header <numeric>: [pre2] #include <numeric> //... std::vector<int> v( 10 ); std::iota( v.begin(), v.end(), 0 ); [/pre2] 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. [pre2] #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; } [/pre2] You can not do the same using MS VC++ 2010. You should use some approach with loops and/or algorithms. For example [pre2] #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; } [/pre2] The output is [pre2] 1 2 3 4 5 6 7 8 9 0 1 2 [/pre2]

Сыроежка: As for the undo command then I can say nothing. It depends on the application itself.



полная версия страницы