2. Convertion of list to array/ Array Pointer


Example :


Suppose I have a list of int type named : abcList.

#include


typedef std::list LIST;

LIST abcList;


// enter few elements in list

abcList.pushback(1);
abcList.pushback(2);

abcList.pushback(3);


// Create aaray of int

const int size =  abcList.size();

int *aarP = new int(size);


LIST::iterator iterBegin = abcList.begin();

LIST::iterator iterEnd = abcList.End();


for (int index = 0; iterBegin != iterEnd; ++iterBegin, ++index) {

 arrP[index] = *iterBegin;

}


for (int index = 0; index < size; ++index) {

 std::cout<<index<<" : "<<arrP[index]<<"\n";

}

Like that you can convert any collection to traditional array.

Thanx.

Comments

Popular Posts