Help needed with vector, list and array please

Pages: 12
yea we didn't really cover STL arrays in class but we did go over iterators. think a look at the reference section on this site would bring me up to speed?

well, thats not for experienced programmers.

That's a really bogus statement. The whole thread has been about using advanced features such as std::vector, std::list and iterators. Using std::array is no more advanced than any of those features. One of the biggest benefits of using std::array is that it acts, in most ways, like any of the other standard containers, and if you use the at() member function you also get array index error checking, which is not present in raw arrays.
you have to keep in mind the OPs level. untill now he dont know how to copy a normal array to another - at this moment i think, recommending him STL array, would not be great.

I disagree. I believe that the C++ standard containers can, and maybe should, be taught before their C counter parts. Using the standard containers properly can produce safer programs than programs using raw arrays, user supplied linked lists, and manual dynamic memory.

think a look at the reference section on this site would bring me up to speed?

Yes. Since std::array works similar to most of the other standard containers you should be able to pick up the differences fairly easily. The biggest problem with the std::array is the fact that is a "new" addition to the language that requires a C++11 compiler, but that can also be said for some of the other constructs provided in this topic, the range based loop for example.

another problem is all instructors are not paced with C++11, i heard of teachers still using turbo C++.

isn't it much simpler to use normal array for selection sorting, or isn't a nearly convention to use normal array in loops.

before C++11, programmers lived decades without <array>, so its not that important - no way near to useful features like ranged based for loop.
instead of <array>, general users can get from <vector>. the extra features of <array> to <vector> is a way outside need of a general user (like me or dyay108). how many times have you seen <array> in these beginner threads?


how about now?

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
35
36
37
38
39

#include <iostream>
#include <list>
#include <vector>
#include <array>
using namespace std;

int main ()
{
array<int,10> a = {0,1,2,3,4,5,6,7,8,9};
vector <int> v(a.begin(), a.end());
list<int> lis (a.begin() , a.end());
array<int,10> b = a ;
vector<int> vb (v) ;
list<int> lisb = lis ;

for(int x: a)
	{
		x=x+2;
        cout<<"array"<<endl;
		cout<<x<<endl;
	}

for(int y: vb)
	{
        y=y+3;
        cout<<"vector"<<endl;
		cout<< y <<  endl;
	}

for(int z: lisb)
	{
		z=z+5;
        cout<<"list"<<endl;
		cout<<z <<  endl;
	}
}



can someone please explain the for loops, i've been really enjoying the feedback so far. i learn better by doing as opposed to sitting in class and just looking at codes.

Last edited on
1. Define an array of ints with the ten elements { 0, 1,2,3, 4, 5, 6, 7, 8, 9 }.

when the teacher asks this , in his mind its normal array, although he may not mind STL array - its probably not right to move to STL before knowing basic normal array operations.
can someone please explain the for loops...

http://www.learncpp.com/
part 5.5, 5.6, 5.7
Topic archived. No new replies allowed.
Pages: 12