How to solve "Access Violation" when using iterators? [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 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;}
}

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.

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)

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

Checking user input in Link List (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 a link list here to check for user input to see whether user has input the word before or not.
ListNode *cur = head;
while ( cur != NULL )
{
if ( guess == cur->item )
{
return true;
}
cur = cur->next;
}
return false;
My problem is that even though the list is empty, it'll still enter the while loop. What's my mistake?
Do you initialize the empty list with head=NULL;? Otherwise head will most likely point to some random memory, and it will be impossible to detect that the list is empty.