how to make a vector function?
Feb 13, 2010 at 4:05pm UTC
how do i create a function with return type vector? anyone know the format? thanks
Feb 13, 2010 at 4:07pm UTC
std::vector<int > f();
Feb 14, 2010 at 2:03am UTC
doesn't that just creates a vector? isn't the format for declaring a vector? i want a function.
Feb 14, 2010 at 2:15am UTC
If you notice there is an open and close parenthesis, after f.
this could be rewritten as
1 2 3 4 5
std::vector<int > f(void ){
std::vector<int > foo;
//do some stuff
return foo;
}
I included a body in my declaration...though it doesnt do anything but create a new vector.
Last edited on Feb 14, 2010 at 2:15am UTC
Feb 14, 2010 at 2:41am UTC
i thought for a normal vector declaration the thing inside the parentheses
was the vector size, which was optional, thus std::vector<int> foo(vect_size) , thought its the formal way to declare a vector, it's also the way to declare a vector function?
also i tried the way you guys suggest before, and it gave me some weird stuff that's why i wanted to know if that was really the way to declare a vector function. here was my code, a simple code test the format above:
1 2 3 4
std::vector<int > say ()
{
cout << "lallala" ;
}
when i called on say, it would output "lallala" BUT NOT BEFORE IT OUTPUTTED ALOT OF WEIRD STUFF. heres the output, im on ubuntu btw.
]*** glibc detected *** ./test2: free(): invalid pointer: 0x003b2ff4 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0x2ddff1]
/lib/tls/i686/cmov/libc.so.6[0x2df6f2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x2e279d]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x61f6f1]
./test2[0x80488db]
./test2[0x80488a7]
./test2[0x804881e]
./test2[0x80487af]
./test2[0x804870c]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x289b56]
./test2[0x8048631]
======= Memory map: ========
001a6000-001c1000 r-xp 00000000 08:05 1323 /lib/ld-2.10.1.so
001c1000-001c2000 r--p 0001a000 08:05 1323 /lib/ld-2.10.1.so
001c2000-001c3000 rw-p 0001b000 08:05 1323 /lib/ld-2.10.1.so
001d1000-001f5000 r-xp 00000000 08:05 4310 /lib/tls/i686/cmov/libm-2.10.1.so
001f5000-001f6000 r--p 00023000 08:05 4310 /lib/tls/i686/cmov/libm-2.10.1.so
001f6000-001f7000 rw-p 00024000 08:05 4310 /lib/tls/i686/cmov/libm-2.10.1.so
00273000-003b1000 r-xp 00000000 08:05 4302 /lib/tls/i686/cmov/libc-2.10.1.so
003b1000-003b3000 r--p 0013e000 08:05 4302 /lib/tls/i686/cmov/libc-2.10.1.so
003b3000-003b4000 rw-p 00140000 08:05 4302 /lib/tls/i686/cmov/libc-2.10.1.so
003b4000-003b7000 rw-p 00000000 00:00 0
00567000-0064d000 r-xp 00000000 08:05 8277 /usr/lib/libstdc++.so.6.0.13
0064d000-00651000 r--p 000e6000 08:05 8277 /usr/lib/libstdc++.so.6.0.13
00651000-00652000 rw-p 000ea000 08:05 8277 /usr/lib/libstdc++.so.6.0.13
00652000-00659000 rw-p 00000000 00:00 0
0088f000-00890000 r-xp 00000000 00:00 0 [vdso]
00d6a000-00d86000 r-xp 00000000 08:05 1373 /lib/libgcc_s.so.1
00d86000-00d87000 r--p 0001b000 08:05 1373 /lib/libgcc_s.so.1
00d87000-00d88000 rw-p 0001c000 08:05 1373 /lib/libgcc_s.so.1
08048000-08049000 r-xp 00000000 08:05 133424 /home/jinjin/Desktop/test2
08049000-0804a000 r--p 00000000 08:05 133424 /home/jinjin/Desktop/test2
0804a000-0804b000 rw-p 00001000 08:05 133424 /home/jinjin/Desktop/test2
08af7000-08b18000 rw-p 00000000 00:00 0 [heap]
b7818000-b781a000 rw-p 00000000 00:00 0
b782b000-b782e000 rw-p 00000000 00:00 0
bfa81000-bfa96000 rw-p 00000000 00:00 0 [stack]
allalalAborted
Last edited on Feb 14, 2010 at 2:42am UTC
Topic archived. No new replies allowed.