Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector< std::vector<int> > my2dArray;
- std::vector<int> mySizes;
- for (int i = 0; i < my2dArray.size(); i++ )
- {
- mySizes.push_back( my2dArray[i].size() );
- }
- typedef std::vector<int> IntVec;
- std::vector<size_t> sizes(my2dArray.size());
- std::transform(my2dArray.begin(), my2dArray.end(), sizes.begin(),
- std::mem_fun_ref(&IntVec::size));
- std::vector<int> mySizes(my2dArray.size());
- auto getSize = [](const std::vector<int> & v) { return v.size(); };
- std::transform(my2dArray.begin(), my2dArray.end(), mySizes.begin(), getSize);
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- class Sizes
- {
- public:
- Sizes( std::vector<int>& v ) : sizeVec( v ) {}
- void operator() ( std::vector<int>& v ) {
- sizeVec.push_back( v.size() );
- }
- std::vector<int>& sizeVec;
- };
- void outFunc (int i) {
- std::cout << " " << i;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- std::vector<std::vector<int>> twodVec;
- std::vector<int> vec;
- vec.push_back( 6 );
- twodVec.push_back( vec );
- vec.push_back( 3 );
- twodVec.push_back( vec );
- vec.push_back( 8 );
- twodVec.push_back( vec );
- vec.push_back( 3 );
- twodVec.push_back( vec );
- std::vector<int> sizeVec;
- Sizes sizes(sizeVec);
- std::for_each( twodVec.begin(), twodVec.end(), sizes );
- std::for_each( sizeVec.begin(), sizeVec.end(), outFunc );
- return 0;
- }
- class Sizes
- {
- public:
- void operator() ( std::vector<int>& v ) {
- sizeVec.push_back( v.size() );
- }
- std::vector<int> sizeVec;
- };
- Sizes sizes;
- sizes = std::for_each( twodVec.begin(), twodVec.end(), sizes );
- std::for_each( sizes.sizeVec.begin(), sizes.sizeVec.end(), outFunc );
Add Comment
Please, Sign In to add comment