Help needed with vector, list and array please

Pages: 12
I need to do the following
1. Define an array of ints with the ten elements { 0, 1,2,3, 4, 5, 6, 7, 8, 9 }.
2. Define a vector<int> with those ten elements.
3. Define a list<int> with those ten elements.
4. Define a second array, vector, and list, each initialized as a copy of the
first array, vector, and list, respectively.
5. Increase the value of each element in the array by 2; increase the value of
each element in the vector by 3; increase the value of each element in the
list, by 5.

So far I think I've done the first 3 correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <list>

using namespace std;

int arr[10];
vector<int> arr[10];
list<int> arr[10];

int main ()
{
    
}
Last edited on
You're syntax is correct, but you've given all three the same name "arr".
I thought i was giving them the same elements.

Would this be more appropriate then?

int arr[10];
vector<int> vec[10];
list<int> lis[10];

Thank you for your time
You really shouldn't be using the global variables either, put the definitions inside main(). And you haven't assigned the desired values ({ 0, 1,2,3, 4, 5, 6, 7, 8, 9 }) to any of those data structures.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <list>

using namespace std;

int main ()
{

int array [] = {0,1,2,3,4,5,6,7,8,9};

vector<int> v = {0,1,2,3,4,5,6,7,8,9};

list<int> lis = {0,1,2,3,4,5,6,7,8,9}; 


}


would this be more appropriate?
2. Define a vector<int> with those ten elements.

so, instead of vector<int> v = {0,1,2,3,4,5,6,7,8,9};
use, vector<int> v (begin(array),end(array));
or, vector<int> v (array, array+10);
Last edited on
Don't forget to #include the vector header. You could also use the std::array instead of the raw array.

thx guys.

now for step 4, if i understand it correctly, it's asking to create a new vector,array, list, and copy the content of the previous ones into the newly created ones correspondingly?

so

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
#include <iostream>
#include <list>
#include <vector>

using namespace std;

int main ()
{

int array <int,10> a = {0,1,2,3,4,5,6,7,8,9};

vector<int> v  (begin(array),end(array));

list<int> lis = {0,1,2,3,4,5,6,7,8,9}; 

int array <int,10> b = a ;

vector<int> vb  (v) ;

list<int> lisb = lis ;




}
Last edited on
IMO use normal array. STL array (must include <array>) was introduced in C++11. both array are fixed sized. when you need greater functionality than normal array such as dynamic size etc, use vector (or other STL container)
1
2
int nomal_ar[5] = {1,2,3,4,5}; // normal array
array<int,5> stl_ar = {1,2,3,4,5}; // stl array, #include <array> 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// basic list
#include <iostream>
#include <list>
using namespace std;

int main()
{	
	list<int> li = {0,1,2,3,4,5,6,7,8,9};
	
	for (int i=10; i<100; i++)	
		li.push_back(i); //insert new element at the end
			
	cout << "elements in the list= " << li.size() << "\n";
	
	for(int x: li)
		cout << x << " "; // prints 0 to 99
       
	return 0;
}
Last edited on
i was able to clean up the code and build it, now i just need help with step 5

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
#include <iostream>
#include <list>
#include <vector>



using namespace std;

int main ()
{

int a [10]  = {0,1,2,3,4,5,6,7,8,9};

vector <int> v(a , a+10);

list<int> lis (a,a+10);

int b = *a ;

vector<int> vb (v) ;

list<int> lisb = lis ;


}
use loop for modifying, ie
1
2
3
4
5
6
	for(int &x: lis)
	{
		x=x*2;
		cout << x << " ";
	}
		
thank you everyone
I'd do something like this for the first three items.
1
2
3
4
5
6
   // Create an array with the following elements.
   std::array<int,10> myArray =  { 0, 1,2,3, 4, 5, 6, 7, 8, 9 };
   // Create a vector with the same elements as the above array.
   std::vector<int> myVec(myArray.begin(), myArray.end());
   // Create a list with the same elements as the array.
   std::list<int> myList(myArray.begin(), myArray.end());


Then something like this for the fourth item:
1
2
3
4
5
6
7
8
9
   // Create a copy of the above array.
   std::array<int,10> array2 = myArray;
   // Or:
   //    std::array<int, 10> array2;
   //    std::copy ( myArray.begin(), myArray.end(), array2.begin() );
   // Create a copy of the above vector.
   std::vector<int> myVec2(myVec);
   // Create a copy of the above list.
   std::list<int> myList2(myList);


Then something like this for the last.

1
2
3
4
5
6
7
8
9
   // Add two to each array element.
   for(auto itr : array2)
      itr += 2;
   // Add three to each vector element.
   for(auto itr : myVec2)
      itr += 3;
   // Add five to each list element.
   for(auto itr : myList2)
      itr += 5;


IMO use normal array.

Why? A std::array has features that make this program easier to create and use, such as iterators.

this is what i came up with
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
 #include <iostream>
#include <list>
#include <vector>
using namespace std;

int main ()
{
int a [10]  = {0,1,2,3,4,5,6,7,8,9};
vector <int> v(a , a+10);
list<int> lis (a,a+10);
int 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;
	}
}

I doubt that this does what you think:

int b = *a ;
b = the contents of a? no?
1
2
3
int a [3]  = {5,6,7};
int b= *a; // *a=5
cout << b; // 5  ...b is a single int, not an array 
Last edited on
im having trouble initializing the second array with the first one using normal array. should i use the stl array?
im having trouble initializing the second array with the first one using normal array

use for loop

Why? A std::array has features that make this program easier to create and use, such as iterators.

well, thats not for experienced programmers. 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.
Pages: 12