Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to define, initialize and print a list in C++

5 Answers

0 votes
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<int> lst = { 1, 2, 3, 4, 5 };

	for (auto e : lst)
		cout << e << ' ';

	cout << endl;

	return 0;
}


/*
run:

1 2 3 4 5

*/

 



answered Apr 20, 2018 by avibootz
edited Apr 21, 2018 by avibootz
0 votes
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<char> lst = { 'a', 'b', 'c', 'd' };

	for (auto e : lst)
		cout << e << ' ';

	cout << endl;

	return 0;
}


/*
run:

a b c d

*/

 



answered Apr 20, 2018 by avibootz
edited Apr 21, 2018 by avibootz
0 votes
#include <iostream>
#include <iterator>
#include <list>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<char> lst = { 'a', 'b', 'c', 'd', 'e' };

	copy(begin(lst), end(lst), std::ostream_iterator<char>(std::cout, " "));

	cout << endl;

	return 0;
}


/*
run:

a b c d e

*/

 



answered Apr 20, 2018 by avibootz
0 votes
#include <iostream>
#include <iterator>
#include <list>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<char> lst = { 'a', 'b', 'c', 'd', 'e', 'f' };

	list<char>::iterator p;

	while (!lst.empty()) {
		p = lst.begin();
		cout << *p << ' ';
		lst.pop_front();
	}

	cout << endl;

	return 0;
}


/*
run:

a b c d e f

*/

 



answered Apr 20, 2018 by avibootz
0 votes
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

int main()
{
	list<int> lst = { 1, 2, 3, 4, 5 };

	for (list<int>::iterator p = lst.begin(); p != lst.end(); p++)
		cout << *p << " ";

	cout << endl;

	return 0;
}


/*
run:

1 2 3 4 5

*/

 



answered Apr 21, 2018 by avibootz

Related questions

2 answers 172 views
2 answers 158 views
3 answers 187 views
2 answers 164 views
1 answer 152 views
1 answer 195 views
...