how can I copy my array into a list?

Hi all,

from the reference section on lists, I performed the following operation:
Initialize an array of consecutive digits.
Copy that array into a list object.

From the code below, what is happening is that the pointer I've created is being copied, but not the array. So basically what is being copied is the first element of the array, value-wise. My array is populated at run-time so my code is a bit different from the example provided in the list referecnce on this site:
http://www.cplusplus.com/reference/stl/list/list/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int mySides = 4;
    int * tempSet = new int[mySides]; 

    //initialize tempset;
    for (int i = 0; i < mySides; i++) {
        tempSet[i] = i;
        showDebugMsg("array is: ",tempSet[i]); //outputs values 0,1,2,3 when run.
    }
    showDebugMsg("sizeof tempset is: ",sizeof(tempset)); //outputs '4', the size of the first element.
    //copy the tempset into the list.
    list<int> tempList (tempSet, tempSet + sizeof(tempSet) / sizeof(int) );//only copies the first element, behaves wierd since I'm creating a size of one.

    for (list<int>::iterator it = tempList.begin(); it != tempList.end(); it++) {
         showDebugMsg("value is: ", *it); //Prints out zero, the first element. (After initializing each element with a size of one?
    }


the sample code in the reference creates a static array. Do I need to manually set the size of my array being copied?
In this case you should do it like this -> list<int> tempList (tempSet, tempSet + mySides);

You'll say "but I saw the list reference and I swear it did it like I do it here!..." (*)
True, but notice that the example there uses an array. You use a pointer here. There's a difference...

Take a look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
    int a[10];
    int *pa;

    cout << "sizeof(a)=" << sizeof(a) << endl;
    cout << "sizeof(a)/sizeof(int)=" << sizeof(a)/sizeof(int) << endl;

    cout << "\nsizeof(pa)=" << sizeof(pa) << endl;
    
    pa=new int[10];
    cout << "sizeof(pa)=" << sizeof(pa) << endl;
    
    delete pa;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

(*) http://cplusplus.com/reference/stl/list/list/
Last edited on
Right I did notice the difference between the static array and the pointer in my case, and noticed that only the size of the pointer itself (first element) was being returned by sizeof. I was just curious how to tell the list constructor that. I've got it though. thanks for the update (I did think they were the same.)
Topic archived. No new replies allowed.