How does one peek and advnace a range inside a foreach loop - d

Inside a foreach loop which iterates over a range, I want to (1) advance and (2) peek over the next element in the range without advancing it.
import std.range: splitter;
import std.conv: parse;
foreach(numstr; line.splitter(',')) {
const int code = parse!int(numstr);
switch (code) {
case 1:
auto next1 = // currentRange.next()
// calling next() advances the range
auto next2 = // currentRange.next()
auto next3 = // currentRange.next()
// ...
case 2:
auto next1 = // currentRange.peek()
// calling peek() will not forward the range
currentRange.advanceBy(4);
// ...
// ...
}
}

(1) advance
You could advance the range manually with popFront, but I would not recommend combining that with a foreach loop. Perhaps replace the foreach with while (!range.empty) ?
(2) peek over the next element in the range without advancing it
For this, advance a copy of it:
range.save.dropOne.front
range.save.drop(4).front
Of course, the splitter will have to redo the work for each peek. To avoid this, save its result to an array, or use split.

Related

How to get exactly one element from an unordered_set?

I have:
std::unordered_set<ObjectRepresentation*> incompletePieces;
I would like to get exactly one object from the unordered_set. To do that I am using a for loop, and "break", at the end of the loop so that the loop runs at most once.
while (incompletePieces.size()){
for (auto containedPiece : incompletePieces){ //Warning at this line that loop will run at most once
// .... doing some stuff with the contained piece
incompletePieces.erase(containedPiece);
break;
}
}
This is the desired behaviour that I want. The problem is that the compiler shows a warning:
Loop will run at most once (loop increment never executed)
How do I rewrite my code so that the warning goes away ? Is there a better way to get an item from the unordered_set ?
You could use begin() to get the first element.
if (incompletePieces.size() > 0)
auto containedPiece = *(incompletePieces.begin());
The code you presented does in fact process all elements and clears the set of them as it gets done, but it does so in a highly unidiomatic way.
There are two idiomatic ways of doing this, depending on whether processing an element could modify the set itself.
1) If the "doing some stuff" code is guaranteed to not touch incompletePieces (i.e. completing one piece does not create additional incomplete pieces), then the idiomatic and efficient solution is to just loop over the set and clear it afterwards:
for (auto piece : incompletePieces) {
// process piece
}
incompletePieces.clear();
2) If this is not the case, or you really need to clear elements as you go, then the idiomatic solution is still iterator based looping:
auto it = incompletePieces.begin();
while (it != incompletePieces.end()) {
// process *it
#if C++11
it = incompletePieces.erase(it);
#else
auto prev = it++;
incompletePieces.erase(prev);
#endif
}
Whereas *unordered_set::begin() will give you first element (no unordered_set::front()),
I would rewrite:
while (incompletePieces.size()){
for (auto containedPiece : incompletePieces){
// .... doing some stuff with the contained piece
incompletePieces.erase(containedPiece);
break;
}
}
into:
for (auto* containedPiece : incompletePieces){
// .... doing some stuff with the contained piece
}
incompletePieces.clear();
You can rewrite the code as below:
for(auto* containedPiece : incompletePieces){
//Process the set contents
}
//Clear entire set in one go
incompletePieces.clear();
If you want to clear it one by one, you would have to use iterators as shown below:
auto it = incompletePieces.begin(); //Take the pointer to first element of set
for( ; it !=incompletePieces.end() ; it++){
incompletePieces.erase(*it); //Erase one element at a time
}

Start value for vector loop

Say I have the following:
int numFields = 0;
for ( auto & isFieldBlank : InputProcessor::numericFields_isBlank ) {
if ( !isFieldBlank ) {
numFields += 1;
}
}
InputProcessor::numericFields_isBlank is a bool vector of all numeric input values indicating whether the input values are empty true or populated false.
I have two related questions:
Is there a better way to count the populated fields in the vector?
Is there a way to provide a starting index to the for loop iterator?
A range based for loop will always run the entire range, you cant change that unless you adapt the range. What you can do though is use std::count/std::count_if to count the instances for you like
auto count = std::count(container.begin(), container.end(), true);
and to change the start and stop positions you can use std::next to move the iterator like
auto count = std::count(std::next(container.begin(), some_value),
std::next(container.end(), -some_other_value),
true);
Is there a better way to count the populated fields in the vector?
You can simplify the body of the for loop to:
numFields += (!isFieldBlank);
The complete for loop will be
for ( auto & isFieldBlank : InputProcessor::numericFields_isBlank ) {
numFields += (!isFieldBlank);
}
Is there a way to provide a starting index to the for loop iterator?
You certainly can. However, you will need to use a normal for loop, not a range-for loop.

Iterating through a C++ vector with wrap-around

I have an assignment to iterate through a vector and erase every third number. If it hits the end of the vector, it should continue counting again from the first entry, until only one number remains. The user inputs how many numbers should be in the vector.
I'm having trouble getting used to the difference between vectors and arrays - just last week we had a problem that involved wrapping around an array, which was solved with mod, but I quickly figured out this wouldn't work for vectors.
Here was my idea so far: Iterate through and delete every third entry until the size of the vector is 1.
while (vector.size > 1) {
for(std::vector<int>::iterator i = suitors.begin(); i <= suitors.end(); i++) {
// here, add a case for if it hits the end, start over
if (i = suitors.end()) {
i = suitors.begin();
}
suitors.erase(suitors.at(i) + 2);
}
The problem I'm having is figuring out how to have it start over, as spits out an error when I try to use i in this way.
Any advice or tips to get me on the right path here? I'm beginning to see how versatile vectors are, but they just aren't clicking yet. I'm also not sure if there is a better way to stop it from iterating besides the while loop.
I'd use remove_if to move items in the vector to the end whenever an index variable that is incremented each time reaches 3.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v{1,2,3,4,5,6};
unsigned index = 0; // this is the index variable used to remove elements
auto end = v.end(); // point to the current end of the vector
// keep looping until there is only 1 element in the vector
while(std::distance(v.begin(), end) > 1) {
// remove_if will call the predicate for each element
// the predicate simply increments the index each time, and when it reaches
// 3 indicates that element should be removed
// remove_if will move items to the end of the vector and return an
// iterator to the end of the new range, so we'll update the end variable
// with the result
end = std::remove_if(v.begin(), end, [&index](int) {
if(++index == 3) {
// reset the index and indicate this element should be removed
return (index = 0), true;
}
return false;
});
for(auto iter = v.begin(); iter != end; ++iter) {
std::cout << *iter << ' ';
}
std::cout << '\n';
}
// erase all the elements we've removed so far
v.erase(end, v.end());
}
Output:
1 2 4 5
1 2 5
1 5
1
Live demo
The outer while loop I'm assuming means to go as long as the vector has more than one element, but this should be included in the for, not another loop
The syntactic issue is in the if:
if (i = suitors.end())
// ^ should be ==
otherwise you're just assigning end to your iterator
for(std::vector<int>::iterator i = suitors.begin(); suitors.size() > 1; ++i) {
// ^ loop condition changed
if (i == suitors.end()) {
i = suitors.begin();
}
suitors.erase(suitors.at(i) + 2);
}
modifying a container as you iterate through it is dangerous though..

How to extract an element from a deque?

Given the following code :
void World::extractStates(deque<string> myDeque)
{
unsigned int i = 0;
string current; // current extracted string
while (i < myDeque.size()) // run on the entire vector and extract all the elements
{
current = myDeque.pop_front(); // doesn't work
// do more stuff
}
}
I want to extract each iteration the element at the front , but pop_front() is a void
method . How can I get the element (at the front) then ?
Regards
Use front to read the item and pop_front to remove it.
current = myDeque.front();
myDeque.pop_front();
This way of doing things may seem counter-productive, but it is necessary in order for deque to provide adequate exception-safety guarantees.

What is proper way to delete objects that resides in a list that you find while looping that list?

I have a list of Star structs. These structs are in a std::list
I am double looping this list and compairing there locations to detect a collision. When A collision is found I will delete Star with the lowest mass. But how can I delete the Star when I am in the double Loop, and keep the loop going to check for more collisions?
It's worth mentioning that the second loop is a reverse loop.
Here is some code
void UniverseManager::CheckCollisions()
{
std::list<Star>::iterator iStar1;
std::list<Star>::reverse_iterator iStar2;
bool totalbreak = false;
for (iStar1 = mStars.begin(); iStar1 != mStars.end(); iStar1++)
{
for (iStar2 = mStars.rbegin(); iStar2 != mStars.rend(); iStar2++)
{
if (*iStar1 == *iStar2)
break;
Star &star1 = *iStar1;
Star &star2 = *iStar2;
if (CalculateDistance(star1.mLocation, star2.mLocation) < 10)
{
// collision
// get heaviest star
if (star1.mMass > star2.mMass)
{
star1.mMass += star2.mMass;
// I need to delete the star2 and keep looping;
}
else
{
star2.mMass += star1.mMass;
// I need to delete the star1 and keep looping;
}
}
}
}
}
You need to utilize the return value of the erase method like so.
iStar1 = mStars.erase(iStar1);
erase = true;
if (iStar1 == mStars.end())
break; //or handle the end condition
//continue to bottom of loop
if (!erase)
iStar1++; //you will need to move the incrementation of the iterator out of the loop declaration, because you need to make it not increment when an element is erased.
if you don't increment the iterator if an item is erased and check if you deleted the last element then you should be fine.
Since modifying the list invalidates the iterators (so that you cannot increment them), you have to keep safe the iterators before the list is changed.
In the most of the implementation std::list is a dual-linked list, hence a iteration like
for(auto i=list.begin(), ii; i!=list.end(); i=ii)
{
ii = i; ++ii; //ii now is next-of-i
// do stuff with i
// call list.erasee(i).
// i is now invalid, but ii is already the "next of i"
}
The safest way, is to create a list containing all the "collided", then iterate on the "collided" calling list.remove(*iterator_on_collided)
(but inefficient, since has O2 complexity)
You want to use the result of erase() to get the next iterator and advance the loop differently:
If you erase using the outer iterator you clearly can abondon checking this Star against others and break out of the inner loop. Only if the inner loop was complete you'd want to advance the outer iterator because otherwise it would be advanced by the erase().
If you erase using the inner loop you already advanced the iteration, otherwise, i.e. if no star was erased, you need to advance.
Sample code would look somethimg like this:
for (auto oit(s.begin()), end(s.end()); oit != end; )
{
auto iit(s.begin());
while (iit != end)
{
if (need_to_delete_outer)
{
oit = s.erase(oit);
break;
}
else if (need_to_delete_inner)
{
iit = s.erase(iit);
}
else
{
++iit;
}
}
if (iit == end)
{
++oit;
}
}