std::cout not giving out vector contents...

Write your question here.
ok so this is just a part of the whole code... after running
i don't get to see content of the vector on terminal...
but the size comes out as 24...
????
1
2
3
4
5
6
7
8
9
10
11
12
13
 std::cout<<std::endl;
std::cout<<"size of last vector is "<<sizeof(values_to_play)<<std::endl;

std::cout<<"this are values to play:"<<std::endl;
for(auto ptr = values_to_play.begin(); ptr != values_to_play.end(); ptr++)
{
 std::cout<<values_to_play[*ptr]<<", ";

}

std::cout<<std::endl;

std::cout<<"values to play size is  "<<sizeof(values_to_play);



this is what appears on terminal? Isn't it strange ?
1
2
3
4
5
6
7
8
9
10
11
12
13
size of last vector is 24
this are values to play:

values to play now 24
row - minus is 300


THIS IS NO. OF ROWS 300
Process returned 0 (0x0)   execution time : 0.029 s
Press ENTER to continue.


There are several errors in that code you have posted.

1) sizeof does not get the length of the vector, it gets the size of vector object. Use .length instead: values_to_play.length()
2) When using iterators, all you need to do is dereference the iterator: std::cout<<*ptr<<", ";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> values_to_play { 0, 1, 2, 3, 4, 5 };

    // option one: range based for
    std::cout << "size: " << values_to_play.size() << "    values: [ " ;
    for( int v : values_to_play ) std::cout << v << ' ' ;
    std::cout << "]\n" ;

    // option two: iterate using positions
    std::cout << "size: " << values_to_play.size() << "    values: [ " ;
    for( std::size_t i = 0 ; i < values_to_play.size() ; ++i ) std::cout << values_to_play[i] << ' ' ;
    std::cout << "]\n" ;

    // option three: iterator
    std::cout << "size: " << values_to_play.size() << "    values: [ " ;
    for( auto iter = values_to_play.begin() ; iter != values_to_play.end() ; ++iter ) std::cout << *iter << ' ' ;
    std::cout << "]\n" ;

    // option four: std::for_each with a callable object
    std::cout << "size: " << values_to_play.size() << "    values: [ " ;
    std::for_each( values_to_play.begin(), values_to_play.end(), []( int v ) { std::cout << v << ' ' ; } ) ;
    std::cout << "]\n" ;

    // option five: std::copy with an output stream iterator
    std::cout << "size: " << values_to_play.size() << "    values: [ " ;
    std::copy( values_to_play.begin(), values_to_play.end(), std::ostream_iterator<int>( std::cout, " " ) ) ;
    std::cout << "]\n" ;
}

http://coliru.stacked-crooked.com/a/71e3ab3995efe202
Topic archived. No new replies allowed.