What does this cycle do?What is the point? [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 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;
}
);

Related

What does 'dict' do in c++? [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 3 months ago.
Improve this question
I was looking at a solution for this problem:
Given a string s, find the length of the longest substring without repeating characters.
The following solution was posted, but I am having trouble understanding what dict does. I've tried looking for documentation in C++. However, I have not found anything. Can someone explain how it works, and where I can find documentation?
int lengthOfLongestSubstring(string s) {
vector<int> dict(256, -1);
int maxLen = 0, start = -1;
for (int i = 0; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}
dict is just the name that was used for this vector<int>, first parameter is the the size of vector, second is value that should be assigned to all of its positions.
This is one of the possible ways to use its constructor, check the example on this page.

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)

Find the closest object from your position on a proper way QT c++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to find the closest Healthpacks or enemy from a player's position. I have wrote this like:
for(auto &hp : model->getAllHealthPacks()){
if(!hp->getUsed()){
int x = hp->getXPos();
int y = hp->getYPos();
int q = (x*x)+(y*y);
if(q < smallest){
smallest = z;
hpfound = hp;
foundAHp++;
}
}
}
Now I was wondering, this is actually not proper. Are there better and profesional way's to improve my code? (Lambda,...)?
The code in general is not bad, but there is some room for improvement. First, you could make the variable hp a constant since you are not modifying its contents.
You could also create a class to store the coordinates in a single object like this
class Coordinate{
std::pair<int,int> coords;
...
};
The final code could look like this:
for(const auto &hp : model->getAllHealthPacks()){
if(!hp->getUsed()){
Coordinate coord(hp->getCoord());
int q = coord.getX()*coord.getX()+coord.getY()*coord.getY();
if(q < smallest){
smallest = z;
hpfound = hp;
foundAHp++;
}
}
}
You should also rename q to something more clear for future reference.

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;}
}

make insert one by one in order C++ [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 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);