For those new to message boards please try to follow a few simple rules when posting your question.
Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
Keep the subject line brief, but descriptive. eg "File Serialization problem"
Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
No advertising or soliciting.
We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
You are correct that begin() will initialize an iterator, and that the ++ operator will advance it. I'm not sure why you are storing iterators though - if you want fast random access you might be better off with a std::vector.
But if all you want to do is add items to the list, push_front() and push_back() (or emplace_front() and emplace_back()) are easier to use. insert() is more useful for adding items in the middle of the list.
with insert when I add items and then use the ++ operator on the iterator I get an exception ? that I went past the end
what I am trying to do is add item 1 and have on the top of the list add item 2 and have it the second on the list.
not sure push_back do that with push_back if for arguments sake there are lets say room for 10 items and I add 3 so the first would be number 10 and when adding the second
it would be number 9 and when adding the 3rd it would number 8. If later on I would like to retrieve the first I would have to know that the first is number 8 How would I reference that with an iterator ?
If you want the first element of a list you can use list.front() and for the last element list.back(). The push_front() function adds elements at the start of the list and push_back() adds them at the end.
The list doesn't start off with any fixed length, it grows and shrinks as you add and remove elements.
So if I was adding two items to a list I would do something like this:
Okay then how I would traverse the list. If I do pop_front its gets the first element but the doc says it DELETES it as well I want to maintain the list I have it associated with a items in a dropdown combo box.
When using insert you are providing the iterator my question is how do get value after I do list::begin to get the first when I bump it up with ++ operator I get an exception that I want past the end.
It appears from my test that using a saved iterator will yield inconsistent results. I have a list with a single element. After adding an item before the saved iterator it still points to the first element; i.e. not the newly inserted value. If I then increment the iterator it points to an invalid address. This is reasonable as the begin and end iterators are dynamically adjusted as the list increases or decreases. So the moment you add or remove an item, your saved iterator can no longer be relied upon. The take home message is - don't do it this way, use the proper member functions of the std::list.
Richard not sure how you add items with insert I get the initial pointer from begin but incrementing it with the ++ operator doesn't yield valid results
I think the push_back will have things in the right order
I am not sure what value the iterator should have when using insert to add an item with push_back I think the method allocates the storage
I think you missed the point. The begin and end iterators are dynamic and are recalculated every time you insert or remove an element from the list. In your case you capture the begin iterator which points to the first element of the list. You then insert an item at the front of the list, so your saved iterator is no longer valid. You then increment it so it could, quite reasonably, point beyond the end of the list. To use iterators properly you must call the begin and end methods of the list each time you need their values.
The iterator is controlled by the template class. So to get the current value of an iterator you must call begin or end. As I said previously these values are not fixed, but must be recalculated each time the list changes.
You can easily test this with a simple list of integer. Do some inserts and deletes and display the saved iterators after each action.
In order to do an insert I have to get a value for a iterator as the documentation says you provide that to insert
In all my google searches I have never seen how that’s done I understand begin starts a iterator with a initial value how do I get a value for it when inserting the second or third item the using the ++ operator gives me an exception
As I keep saying: To get an iterator you must call one of the functions listed under the title Iterators at std::list - cppreference.com[^]. If you then insert an element in front of the iterator then it is no longer valid. So before you increment it call begin a second time to ensure you have the current value.
If you create a list of your values first using push_back, then you can add them in one go at whatever point you need in your main list. Or you could use push_front passing your elements in reverse order.
Can somebody smarter than me explain it and help me solve it.
You don't need someone smarter - you just need to read the error message and think about what it is telling you.
I am not a C++ programmer, but it looks like you are trying to see if the contents of variable 'source' matches a pattern in a Regular Expression. However, this has no meaning because the system cannot possibly guess what pattern you want it to match against as you have not told it what pattern you want. My guess is that QRegExp is a class for regular expressions; so you will have to instantiate it, telling it (either in the constructor call or in a method of the instantiated object) what the Regular Expression that you wish to use is.
The error message that you have got is the compiler telling you that you are trying to run a member function (i.e. a method of a class) without having created an object and the function needs an object to use (which presumably has been told what Regular Expression pattern you are wanting to use for the comparison).
I have a template array class based on a std::vector. All worked well w/ c++17, but there is one line I can't seem to port to the new standard. I'm trying to acquire an iterator to the underlying vector.
Any advice is greatly appreciated.
An abbreviated depiction:
template<class Type>class CMy_Array
virtual ~CMy_Array ()
void InsertAt(int index)
// THE LINE IN QUESTION - trying to acquire an iterator...
// this worked w/ std:c++17 -
std::vector<Type>::iterator p = m_vItems.begin();
// With c++20, 2 errors occur;
// C2760 - syntax error: unexpected token 'identifier', expected ';' , and
// C7510 - 'iterator': use of dependent type name must be prefixed with 'typename'
// Using the documentation for C2760, I modified the line in question as so -
std::vector<Type>::iterator p = static_cast<std::vector<Type>::iterator> m_vItems.begin();
// This eliminated the C7510 error, but C2760 remains.