Vector & list

Hello, I just wanted to ask what is list equivalent to vector's :

vector <int> ints;

ints.push_back(12);

cout << ints[0] << endl; // how do you access elements like this in a list?

// is it possible or not?
You can use the front() member function to access the first element.

1
2
3
list<int> ints;
ints.push_back(12);
cout << ints.front() << endl; 

http://en.cppreference.com/w/cpp/container/list/front
Last edited on
Yes, but accessing more elements, for ex:

vector<GameObject> objects;

for(int i = 0; i < objects.size(); i++)
{
GameObject g = objects[i];
}

//my question now is, what is the list version for this??

you might say, that why don't i use vectors instead, but the point is, list have a function called "remove", and vectors don't. "remove" is what i need.
Last edited on
A list doesn't have the ability to randomly access the elements, you need to iterate over the list one element at a time.

Can you show me how?
this list version of :


vector<GameObject> objects;

for(int i = 0; i < objects.size(); i++)
{
GameObject g = objects[i];
}
Last edited on
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
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <iterator>

template < typename SEQ > void print_twice( const SEQ& seq )
{
    // iterate through the sequence using a range-based loop
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& v : seq ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    // iterate through the sequence using iterators
    // https://cal-linux.com/tutorials/STL.html
    // http://en.cppreference.com/w/cpp/iterator/begin
    for( auto iter = std::begin(seq); iter != std::end(seq) ; ++iter ) std::cout << *iter << ' ' ;
    std::cout << "\n\n" ;
}

int main()
{
    const int a[] { 12, 78, 42, 45, 67, 56, 21, 84, 36, 91, 59 } ;
    print_twice(a) ;

    const std::vector<int> b( std::begin(a), std::end(a) ) ;
    print_twice(b) ;

    const std::list<int> c( std::begin(b), std::end(b) ) ;
    print_twice(c) ;
}

http://coliru.stacked-crooked.com/a/3b12918e0fa907ed
Last edited on
The easiest way it to use iterators to iterate through the list. Did you try finding and reading any documentation for std::list. Most of the documentation shows how to traverse and display a list.

Here is a quick example:

1
2
3
4
5
    std::list<std::string> words {"the", "frogurt", "is", "also", "cursed"};
    std::cout << "The contents of words are: ";
    for(auto& itr : words)
        std::cout << itr << " ";
    std::cout << '\n';


Here is my code :
_________________________________________________
void Handler::tick()
{

for (unsigned int i = 0; i < object.size(); i++)
{
GameObject tempObject = object[i];
tempObject.tick();
}
}

void Handler::render()
{
for (unsigned int i = 0; i < object.size(); i++)
{
GameObject tempObject = object[i];
tempObject.render();
}
}

void Handler::addObject(GameObject* tempObject)
{
object.push_back(*tempObject);
}

void Handler::removeObject(GameObject* tempObject)
{
object.remove(/*remove is not available for vector*/);
// i want a simple method that would tell me how to do this.

i want a simple answer that has all of these functions
}



___________________________________________
Not sure why std::list has remove when std::vector doesn't.

std::vector has erase (and so does std::list).

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
36
37
38
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{ 
	std::vector<int> ints{5, 9, 7, 2, 4};
	
	std::cout << "Before: ";
	for (int i : ints)
	{
		std::cout << i << ' ';
	}
	std::cout << '\n';
	
	int n;
	std::cout << "Which number do you want to remove? ";
	std::cin >> n;
	
	std::vector<int>::iterator it = std::find(ints.begin(), ints.end(), n);
	
	if (it != ints.end())
	{
		ints.erase(it);
		std::cout << "The number has been removed!\n";
	}
	else
	{
		std::cout << "The number was not found!\n";
	}
	
	std::cout << "After: ";
	for (int i : ints)
	{
		std::cout << i << ' ';
	}
	std::cout << '\n';
}
Test run #1
Before: 5 9 7 2 4 
Which number do you want to remove? 2 
The number has been removed!
After: 5 9 7 4
Test run #2
Before: 5 9 7 2 4 
Which number do you want to remove? 100 
The number was not found!
After: 5 9 7 2 4


If you already know the index that you want to remove you don't have to search for it. Instead you can just add the index to the begin iterator and pass that to erase.

 
ints.erase(ints.begin() + indexThatYouWantToRemove);
Thank you, but can you tell me what is .remove version for vector?

because I want to remove objects from a vector like you do in a list.

l_____________________________


list.remove(object);
//vector version?
I think the erase-remove idiom is the closest you get to an equivalent operation for std::vector.
https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom

 
vec.erase(std::remove(vec.begin(), vec.end(), object), vec.end());
Topic archived. No new replies allowed.