-
December 24th, 2013, 07:12 PM
#1
list and arrays
using the following header files:
Code:
#include <list>
#include <map>
#include <iostream>
#include <algorithm>
and If there is a creation of a list, how can one find the sizeof the list. and is it possible to copy all the data from the linked list into an array. Assuming that the data is of type
-
December 24th, 2013, 10:15 PM
#2
Re: list and arrays
Originally Posted by Nair69
how can one find the sizeof the list.
Do you literally mean "sizeof()"?
Code:
#include <list>
#include <iostream>
using namespace std;
int main()
{
cout << sizeof(std::list<char>) << endl;
}
That returns the size of the std::list<char> type in bytes.
Or do you mean you want to know the number of items in the list? If so, then look at the std::list<T>::size() function.
and is it possible to copy all the data from the linked list into an array. Assuming that the data is of type
Use std::copy() and make sure the array is sized appropriately so that it can hold all of the elements. To be safer, ditch the array and use a vector for safety.
Code:
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
std::list<char> list1;
list1.push_back('a');
list1.push_back('b');
list1.push_back('c');
vector<char> v;
// copy items to the vector
copy(list1.begin(), list1.end(), back_inserter(v));
// output items in the vector
copy(v.begin(), v.end(), ostream_iterator<char>(cout, " "));
}
http://www.cplusplus.com/reference/algorithm/copy/
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|