You can use a combination of the erase vector erase with the standard std::unique algorithm. (If you want to place the result in another vector or container, then you can use the algorithm std::unique_copy ).
For example,
drv.erase( std::unique( std::begin( drv ), std::end( drv ), []( const auto &a, const auto &b ) { return std::get<4>( a ) == std::get<4>( b ); } ), std::end( drv ) );
Below is a demonstration program.
#include <iostream> #include <iomanip> #include <string> #include <tuple> #include <vector> #include <iterator> #include <algorithm> #include <ctime> typedef unsigned long ULONG; int main() { std::vector<std::tuple<ULONG, ULONG, ULONG, time_t, std::wstring, int>> drv = { { 056, 2328, 94, 1545877351, L"Sasha", 15 }, { 2057, 2328, 94, 1545877351, L"Masha", 15 }, { 2057, 2328, 94, 1545877353, L"Dasha", 15 }, { 2058, 2328, 94, 1545877353, L"Sasha", 15 }, { 2059, 2328, 94, 1545877354, L"Misha", 15 }, { 2059, 2328, 94, 1545877354, L"Misha", 15 } }; for ( const auto &item : drv ) { std::wcout << std::setw( 4 ) << std::get<0>( item ) << ", " << std::setw( 4 ) << std::get<1>( item ) << ", " << std::setw( 2 ) << std::get<2>( item ) << ", " << std::get<3>( item ) << ". " << std::get<4>( item ) << ", " << std::get<5>( item ) << '\n'; } std::wcout << '\n'; std::sort( std::begin( drv ), std::end( drv ), []( const auto &a, const auto &b ) { return std::get<4>( a ) < std::get<4>( b ); } ); drv.erase( std::unique( std::begin( drv ), std::end( drv ), []( const auto &a, const auto &b ) { return std::get<4>( a ) == std::get<4>( b ); } ), std::end( drv ) ); for ( const auto &item : drv ) { std::wcout << std::setw( 4 ) << std::get<0>( item ) << ", " << std::setw( 4 ) << std::get<1>( item ) << ", " << std::setw( 2 ) << std::get<2>( item ) << ", " << std::get<3>( item ) << ". " << std::get<4>( item ) << ", " << std::get<5>( item ) << '\n'; } return 0; }
Its output to the console:
46, 2328, 94, 1545877351. Sasha, 15 2057, 2328, 94, 1545877351. Masha, 15 2057, 2328, 94, 1545877353. Dasha, 15 2058, 2328, 94, 1545877353. Sasha, 15 2059, 2328, 94, 1545877354. Misha, 15 2059, 2328, 94, 1545877354. Misha, 15 2057, 2328, 94, 1545877353. Dasha, 15 2057, 2328, 94, 1545877351. Masha, 15 2059, 2328, 94, 1545877354. Misha, 15 46, 2328, 94, 1545877351. Sasha, 15
On the other hand, perhaps you should immediately choose another container, such as std::set or std::unordered_set .
Below is a demo program that shows how you can select only the unique elements of the source vector in the set std::set without changing the vector itself.
#include <iostream> #include <iomanip> #include <string> #include <tuple> #include <vector> #include <set> #include <iterator> #include <algorithm> #include <ctime> typedef unsigned long ULONG; int main() { std::vector<std::tuple<ULONG, ULONG, ULONG, time_t, std::wstring, int>> drv = { { 056, 2328, 94, 1545877351, L"Sasha", 15 }, { 2057, 2328, 94, 1545877351, L"Masha", 15 }, { 2057, 2328, 94, 1545877353, L"Dasha", 15 }, { 2058, 2328, 94, 1545877353, L"Sasha", 15 }, { 2059, 2328, 94, 1545877354, L"Misha", 15 }, { 2059, 2328, 94, 1545877354, L"Misha", 15 } }; for ( const auto &item : drv ) { std::wcout << std::setw( 4 ) << std::get<0>( item ) << ", " << std::setw( 4 ) << std::get<1>( item ) << ", " << std::setw( 2 ) << std::get<2>( item ) << ", " << std::get<3>( item ) << ". " << std::get<4>( item ) << ", " << std::get<5>( item ) << '\n'; } std::wcout << '\n'; auto cmp = []( const auto &a, const auto &b ) { return std::get<4>( a ) < std::get<4>( b ); }; std::set<std::tuple<ULONG, ULONG, ULONG, time_t, std::wstring, int>, decltype( cmp )> tuple_set( cmp ); tuple_set.insert( std::begin( drv ), std::end( drv ) ); for ( const auto &item : tuple_set ) { std::wcout << std::setw( 4 ) << std::get<0>( item ) << ", " << std::setw( 4 ) << std::get<1>( item ) << ", " << std::setw( 2 ) << std::get<2>( item ) << ", " << std::get<3>( item ) << ". " << std::get<4>( item ) << ", " << std::get<5>( item ) << '\n'; } std::wcout << '\n'; return 0; }
The output of the program to the console:
46, 2328, 94, 1545877351. Sasha, 15 2057, 2328, 94, 1545877351. Masha, 15 2057, 2328, 94, 1545877353. Dasha, 15 2058, 2328, 94, 1545877353. Sasha, 15 2059, 2328, 94, 1545877354. Misha, 15 2059, 2328, 94, 1545877354. Misha, 15 2057, 2328, 94, 1545877353. Dasha, 15 2057, 2328, 94, 1545877351. Masha, 15 2059, 2328, 94, 1545877354. Misha, 15 46, 2328, 94, 1545877351. Sasha, 15