make insert one by one in order C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an unsorted array list and I want to insert this array to my custom data structure one by one. When I create my insert function it went insert in order
test arr is {9,10,7,8,5,6,3,4,1,2};
list after the insert {1,3,5,7,9,10,8,6,4,2}
my insert function block
void insert(value_type d)
{
if (data.size() == 0)
{
data.push_back(d);
}
else if (data.size() > 0)
{
// get data begin
//std::list<value_type>::iterator iter = data.begin();
for (auto iter = data.begin(); iter != data.end();++iter)
{
if(d > *iter)
{
data.push_back(d);
break;
}
else if (d <= *iter)
{
data.insert(iter,d);
break;
}
}
}
}

It would appear you are trying to do the insertion of an insertion sort. The standard library has you covered:
Find where the new element should go in O(logn) - upper_bound is a binary search:
var pos = std::upper_bound(data.begin(), data.end(), d);
Insert the element there:
data.insert(pos, d);
Or even as a 1 liner for your whole function...
data.insert(std::upper_bound(data.begin(), data.end(), d), d);

Related

What does this cycle do?What is the point? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
int myDrawOrder = sprite->GetDrawOrder();
auto iter = mSprites.begin();
for(;iter != mSprites.end(); ++iter)
{
if(myDrawOrder < (*iter)->GetDrawOrder()) //what does this line mean?
{
break;
}
}
GetDrawOrder() returns the position of the sprite in the queue.
The loop iterates over the sprites, and breaks when it finds a spite that should be drawn before the sprite. You didn't share the rest of the code, but presumably something is then done with that sprite (e.g., it's drawn).
Since iter is not local to the loop, it's left after the loop pointing to the first element for which myDrawOrder < (*iter)->GetDrawOrder() is true (or mySprites.end() if there wasn't one).
It's a search operation.
It could also be written thusly:
const int myDrawOrder = sprite->GetDrawOrder();
auto iter = std::find_if(
std::begin(mSprites),
std::end(mSprites),
[&](const auto& sprite) {
return sprite.GetDrawOrder() >= myDrawOrder;
}
);

Can you help me identify which iterator is being used here? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can you help me identify which iterator is being used here? (Trivial, input, output)
int maxIndx = -1;
int maxSize = -1;
for (map<int, set<string> >::iterator itr = partitions.begin(); itr != partitions.end(); ++itr) {
int size = (*itr).second.size();
if (size > maxSize) {
maxSize = size;
maxIndx = (*itr).first;
}
}
std::map is documented as having an iterator typedef that is a LegacyBidirectionalIterator.
(cppreference is not official, but it's free, easy, and pretty darn accurate)

C++ How to Check if Array contents can add up to a specific number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Let's say I got this Array:
int myArray[] = {2,5,8,3,2,1,9};
is ther any way I could check if some of the contents can add up to 20? I managed to check if any two values add up to 20 but I just don't know how to handle it if is irrelevant how many values it needs.
Thank you for your help.
Most of the time you have a situation where you test if any combination of values satisfy a condition, you need to think of using recursion. You recurse through the elements and at each point branch off in two directions: one that considers that element, and one that doesn't. This can be short-circuited to stop looking if one of the branches does satisfy that condition.
Here's a potential solution to your problem
bool can_sum(const int* ptr, int size, int target, int total = 0)
{
// check success
if (total == target)
return true;
// check failure
if (total > target || size == 0)
return false;
return can_sum(ptr+1, size-1, target, total + *ptr) // check with *ptr
|| can_sum(ptr+1, size-1, target, total); // check without *ptr
}
int main()
{
int arr[] = {2,5,8,3,2,1,9};
bool result = can_sum(arr, 7, 20);
return 0;
}

What to do when erase fails to delete the element pointed to by the iterator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In the following code, I try to erase a specific element of templist. However, only the last element of the list is removed. How do I erase that specific element?
for(index1 = templist.begin(); index1 != templist.end();)
{
checkit=templist.end();
--checkit;
if((*index1).origin == (*udit).dest && sumweight + (*index1).weight <= 25000)
{
sumhr += 1 + (*udit).hr;
sumweight = sumweight + (*index1).weight;
stops++;
tour.at(i).push_back((*index1));
if(index1! = checkit)
index1 = templist.erase(index1);
else
{
templist.erase(index1);
index1 = templist.end();
}
}
else
index1++;
}
You asked:
What to do when erase fails to delete the element pointed to by the iterator?
Not sure how you concluded that. Some data supporting that claim would have been useful.
However, your use of iterators is a little buggy. You are incrementing the iterator twice after you erase an element.
A suggested fix:
for(index1=templist.begin(); index1!=templist.end(); /* Don't increment the iterator here */ )
{
if((*index1).origin==(*udit).dest && sumweight + (*index1).weight <=25000)
{
sumhr+=1+(*udit).hr;
sumweight=sumweight+(*index1).weight;
stops++;
tour.at(i).push_back((*index1));
// Erase the item and get the next iterator.
index1 = templist.erase(index1);
}
else
{
// Increment the iterator only when we are not erasing.
++index1;
}
}
Your problem is that after you erase the element from the container the iterator is invalid. To fix this you just need to change your logic a little bit. Since the erase function returns an iterator that references the next element in the container you can use that to your advantage. How exactly you do this in your project is up to you but it should work like the following
if (index1 != checkit)
{
// Remove the item. The iterator returned by "erase" is the next one
// in line so there's no need to manually advance to the iterator with ++
index1 = templist.erase(index1);
}
else
{
// Remove the item and skip to the end.
templist.erase(index1);
index1 = templist.end();
}

How to solve "Access Violation" when using iterators? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a class named CLine, which contains vector<CPoint>. I saved all the points along the line in the vector<CPoint> m_vecPoint. I was trying to traverse the points when the problem occurs.
My code:
for(vector<CLine>::iterator iter = m_vecLine.begin(); iter != m_vecLine.end(); iter++)
{
vector<CPoint>::iterator iter1 = iter->m_vecPoint.begin();
int temp = iter1->x;
}
When I debug this, it downs at int temp = iter1->x saying Access Violation.
What may be wrong?
It could be possible that begin == end
for(vector<CLine>::iterator iter = m_vecLine.begin(); iter != m_vecLine.end(); iter++)
{
vector<CPoint>::iterator iter1 = iter->m_vecPoint.begin();
if(iter1 != iter->m_vecPoint.end())
{ int temp = iter1->x;}
}