How to Iterate Through a List of Objects in C++

How to iterate through a list of objects in C++?

You're close.

std::list<Student>::iterator it;
for (it = data.begin(); it != data.end(); ++it){
std::cout << it->name;
}

Note that you can define it inside the for loop:

for (std::list<Student>::iterator it = data.begin(); it != data.end(); ++it){
std::cout << it->name;
}

And if you are using C++11 then you can use a range-based for loop instead:

for (auto const& i : data) {
std::cout << i.name;
}

Here auto automatically deduces the correct type. You could have written Student const& i instead.

How to iterate through a List of Arrays of Objects C#?

I believe the issue here is that you're only adding a reference to the collisionPair array to your broadPhaseCollisionList on each iteration (you're adding the same reference over and over). Meanwhile, the collisionPair items are getting updated with the latest collision on each iteration.

At the end of your loop, collisionPair contains the last collision pair, and broadPhaseCollisionList contains [collisionCount] items all pointing to the same array.

To fix this, you should set collisionPair to a new array each time before adding it to your list:

if (observerBox != observedBox && TestAABBOverlap(observerBox.aabb, observedBox.aabb))
{
observerBox.colliding = true;
observedBox.colliding = true;

broadPhaseCollisionList.Add(new[] {observedBox, observerBox});

Iterating over a list of objects c++

If you are able to use C++11 or higher, you can use a range for loop to iterate over the contents of the list.

void DisplayDirectoryContents()
{
// Get files and folders from directory
list<File> files = this->getFiles();

// Iterate over each item in the list.
// Use a reference to avoid copying of the items.
for ( File& file : files )
{
cout << file.getFileName();
}
}

iterate through List<object>

If I understood well, you are trying to send the view model from the controller to the view. So if you are using razor your code should be like this

@model ViewModel
@foreach(object country in Model.countries)
{
var Name = country.Name
var Code = country.Abbr
var Currency = country.Currency
}

notice the keyword Model.

Edit

// Code inside your controller should be like this
ViewModel myModel = new ViewModel();
List<object> countries = new List<object>();
countries.Add(new { Name = "United States", Abbr = "US" , Currency = "$"});
countries.Add(new { Name = "Canada", Abbr = "CA", Currency = "$" });

myModel.countries = countries;

return View("yourView", myModel); // you can write just return View(myModel); if your view's name is the same as your action

Hope it helps you.

How to iterate through a list of the pointers of objects in C++

So the iterator functions as a pointer to the underlying data. Meaning lets say I had a vector of integers like below

vector<int> test = {1, 2, 3, 4, 5};
for(auto it = test.begin(); it != test.end()l ++it){
cout << *it << endl;
}

This will do what you desire, you basically need to dereference the iterator to get to the underlying data. So in your case to access the course_code replace

cout<<it->course_code<<endl;

With

cout<<*it<< endl; 

Iterate through a list of objects and point to the node that matches a certain criteria

*openi is of type block, right? So you can simply take the address of that:

block * Q = &*openi;

The &* might seem redundant, but the * is an operator on the iterator, returning a block type, as opposed to scenario's where * is a dereference, and the & references it again.

Or you can store the iterator itself:

std::list<block>::iterator Q = openi;

Or you can store the object by value, assuming you implemented the copy constructor:

block Q = *openi;

Adding an object to an existing list during iteration

Since you have a list, you can iterate by index:

// save the length before we iterate so that we don't iterate into new items
int length = listA.Count;

// loop from 0 to the original length
for (int i = 0; i < length; ++i)
{
var a = listA[i];
if (a.somecondition)
{
listA.Add(YourCloneMethod(a));
}
}


Related Topics



Leave a reply



Submit