Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Vector sorting

Name: Anonymous 2010-10-25 11:35

C++:

vector<vector<int> > herp(2, vector<int>(1000000));

How the fuck sort this by herp[0] values?

Name: sage 2010-10-25 11:47

sage

Name: Anonymous 2010-10-25 12:04

You mean this?

#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::vector<int> > herp(10, std::vector<int>(10));
   
    int count = 10;
    std::transform(herp.begin(), herp.end(), herp.begin(), [&](std::vector<int> v1) -> std::vector<int> { auto tmp = v1; tmp[0]=--count; return tmp; });
   
    std::sort(herp.begin(), herp.end(), [](std::vector<int> v1, std::vector<int> v2) { return v1[0] < v2[0]; });
}



GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>;...
Reading symbols from C:\tmp2\tmptmp\ex\sort/a.exe...done.
(gdb) start
Temporary breakpoint 1 at 0x401452: file main.cpp, line 5.
Starting program: C:\tmp2\tmptmp\ex\sort/a.exe
[New Thread 3316.0xcd8]

Temporary breakpoint 1, main () at main.cpp:5
5    {
(gdb) n
6        std::vector<std::vector<int> > herp(10, std::vector<int>(10));
(gdb) n
8        int count = 10;
(gdb) p herp
$1 = std::vector of length 10, capacity 10 = {
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
(gdb) n
9        std::transform(herp.begin(), herp.end(), herp.begin(), [&](std::vector<int> v1) -> std::vector<int> { auto tmp = v1; tmp[0]=--count; return tmp; });
(gdb) n
11        std::sort(herp.begin(), herp.end(), [](std::vector<int> v1, std::vector<int> v2) { return v1[0] < v2[0]; });
(gdb) p herp
$2 = std::vector of length 10, capacity 10 = {
  std::vector of length 10, capacity 10 = {9, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {8, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {7, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {6, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {5, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {4, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {3, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
(gdb) n
6        std::vector<std::vector<int> > herp(10, std::vector<int>(10));
(gdb) p herp
$3 = std::vector of length 10, capacity 10 = {
  std::vector of length 10, capacity 10 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {3, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {4, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {5, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {6, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {7, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {8, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  std::vector of length 10, capacity 10 = {9, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
(gdb) n
12    }(gdb) n
0x004010db in __mingw_CRTStartup ()
(gdb) quit
A debugging session is active.

    Inferior 1 [process 3316] will be killed.

Quit anyway? (y or n)

Name: Anonymous 2011-02-03 7:53


Don't change these.
Name: Email:
Entire Thread Thread List