|
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.
|
|
|
|
|
So to put into code what you just said
List <node> mynode;
List <node>::iterator it:
It = mynode.begin();
mynode.insert(it,valueref);
It = mynode.begin();
It++;
mynode.insert(it,valueref);
It = mynode.begin();
It+=2;
mynode.insert(it,valueref);
It = mynode.begin();
It+=3;
mynode(it,valueref);
I’ll try this out
Thanks
|
|
|
|
|
There are easier ways of adding more than one element the way you want. Take a closer look at the insert page and maybe use one of:
iterator insert( const_iterator pos, InputIt first, InputIt last );
iterator insert( const_iterator pos, std::initializer_list<T> ilist );
(5) (since C++11)
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.
|
|
|
|
|
My insert is in the middle of for(;;) loop but I understand what you are saying
thanks
|
|
|
|
|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
You asked Member 14968771 wrote: 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).
|
|
|
|
|
Hi -
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
{
private:
std::vector<Type> m_vItems;
public:
CMy_Array()
{
}
virtual ~CMy_Array ()
{
m_vItems.clear();
}
void InsertAt(int index)
{
std::vector<Type>::iterator p = m_vItems.begin();
std::vector<Type>::iterator p = static_cast <std::vector<Type>::iterator> m_vItems.begin();
}
}
|
|
|
|
|
typename std::vector<Type>::iterator p = m_vItems.begin();
or
auto p = m_vItems.begin();
|
|
|
|
|
I see many code projects that enhance the CListBox control, but none that lets each item be another control, like fx CButton. How would one implement that?
|
|
|
|
|
|
The link draws radio buttons, but doesn't really use MFC buttons. I am working on a solution that drops the need for CListBox.
|
|
|
|
|
Create some button controls as listbox's children window, put its on the foreground of the listbox, you have to process some messages to change the control's data, coordinates, etc.
|
|
|
|
|
Hello everybody
I have an SDI solution in MFC. I have created a class with the name of "CLASS A"
and
class CXXView : public CView, public CLASS A
How can I get access to CXXView variables from CLASS A in MFC?
Indeed; DOSE NOT WORK ==> CXXView * pCurrentView = static_cast<cxxview*>(GetActiveView());
Because of this error:
GetActiveView() is undifined (XXView.h is included in CLASS A)
Best Regads
|
|
|
|
|
Member 15033704 wrote: ndeed; DOSE NOT WORK ==> CXXView * pCurrentView = static_cast<cxxview*>(GetActiveView());
Because of this error:
GetActiveView() is undifined
Of course it does not work! GetActiveView is a method of CFrameWnd class, but your "CLASS A" has nothing to do with the CFrameWnd!
BTW, why do you think you need to "get access to CXXView variables from CLASS A"?
|
|
|
|
|
Many thanks for your help.
In fact, I do some calculations in CLASS A, then I want to show the results of those calculations in
CXXView class by using pDC->Textout(........).
I use InvalidateRect(NULL,NULL,FALSE) to redraw the view, but as you know it is not a good idea.
I have defined a CRect in CXXView class that I know those texts are going to be shown in that rect and I want to Invalidate just that rect from CLASS A.
On the other hand, while I am in CLASS A and using InvalidateRect function, this function needs 2 parameters of CXXView class to be done that are HWND and CRect is defined in class CXXView.
Best Regads
|
|
|
|
|
Why are you trying to do it from the "CLASS A" instance?
Just implement it in the CXXView class. Or, if your "CLASS A" already has a method that does what you need then just call it from within the instance of CXXView class (of course, this method must be declared as public or protected, not as private).
|
|
|
|
|
Dear Victor Nijegorodov
Before of all, many thanks for your advice.
The structure of solution is as follows:
Calculations are to be done in CLASS A;
and class CXXView : public CView, public CALSS A
CXXView dose not know when these Calculations are done;
When CLASS A send a message to CXXView, then CXXView execute other methods;
The message is InvalidateRect;
The problem is that CXXView dose not know when these Calculations are done.
On the ther hand, I don't want to define a flag and check it continuously if Calculations are done or not?
so CXXView has to await receiving InvalidateRect message from CLASS A
I tried to define an instance of CXXView class within the CLASS A:
CXXView* pCurrentView;
CRect rect = pCurrentView->m_Rect_zone;
HWND pWnd = pCurrentView->GetSafeHwnd();
InvalidateRect(NULL, NULL, FALSE);
but I was not succeeded because of this compiler error:
Uninitialized variable pCurrentView
One macro or something like that is missing here (CXXView* pCurrentView;) which I am not familiar with that.
Best Regards
|
|
|
|
|
codeNation fresher batch has a tradition of laser tournaments .in laser tag two team play against each other .a team can have any number of players >=1.the two teams can have unequal team member.
for successful completion of this tournament ipshita want that every person must have played every other person as part of different teams.every laser tag match take 30 minutes to complete .now the gaming arena has allowed codeNation to play for a maxima of X hours after which the arena close . given the number of folks in the batch as N you will have to find out if codeNation will be able to finish the tournament before the complex closes?
|
|
|
|
|
So what have you tried, and where are you stuck? If you are expecting someone to do your work for you I am afraid disappointment awaits you.
|
|
|
|
|
For those who enjoy playing with algoritms ... A two-step problem: The simple part, make something that works. The difficult part: Provomg that your solution is the optimal one.
Disclaimer: I have no clue about the second part.
Problem: We have a keypad lock at our work facilities. You present your card, and then if the last four digits typed are a valid entry code, the door opens. So if a valid code is 2345, and you type 1234, the door doesn't open. If you then add a 5, the door opens.
An algoritm for trying all possible keys is like the first homework assignment in 101 Elementary Programming.
If you have no knowledge of any entry code, but you know that if the last four digits are correct, the door will open, what will be your dialing strategy for making the minimal nunber of keys dialled to get in? What will be the worst case number of digits dialled? Can you prove that this it the theoretically best, that no other algorithm will provide a lower worst case?
What will be the average across all possible entry codes? Can you prove that your algorithm provides the minimal number of total keypresses in the average case?
I do not have any ready-made "right answer" to this problem, just a bunch of implementations of how to make sure I get it if I have forgotten the code with no complexity analysis. I do know the code, so I do get in - don't worry about that! The problem here is not to get in, but the complexity of getting in if you do NOT know the code!
|
|
|
|
|
here is the method to insert a node at the front in the XORed doubly linked list or also known as memory efficient doubly linked list :
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *npx; };
Node *XOR(Node *a, Node *b){
return ((Node *)( (uintptr_t)(a) ^ (uintptr_t)(b) ));
}
void insert(Node **head_ref, int data){
Node *new_node = new Node();
new_node->data = data;
new_node->npx = XOR((*head_ref), nullptr);
if(*head_ref != nullptr){
Node *next = XOR((*head_ref)->npx, nullptr);
(*head_ref)->npx = XOR(new_node, next);
}
*head_ref = new_node;
}
void printList(Node *head){
Node *curr = head, *prev = nullptr, *next;
cout << "Following are the nodes of Linked List: \n";
while(curr != nullptr) {
cout << curr->data << " ";
next = XOR(prev, curr->npx);
prev = curr;
curr = next;
}
}
int main(){
Node *head = nullptr;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
insert(&head, 50);
insert(&head, 60);
printList(head);
return 0;
}
Here is the code i have tried to put an element at the back of the list:
Node *insert(Node **last, int data){
Node *new_node = new Node();
new_node->data = data;
new_node->npx = XOR(*last, nullptr);
if(*last != nullptr) {
Node *prev = XOR((*last)->npx, nullptr);
(*last)->npx = XOR(prev, new_node);
}
return (new_node);
}
int main(){
Node *head = nullptr;
head = insert(&head, 10);
head = insert(&head, 20);
head = insert(&head, 30);
head = insert(&head, 40);
head = insert(&head, 50);
head = insert(&head, 60);
printList(head);
return 0;
}
But it is not working
output: it is same in both the cases
Thank you
|
|
|
|
|
Tarun Jha wrote: But it is not working Debug it and find out what is happening.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I am trying to call two identical functions (one is templated and the other one is normal). The templated one compiles successfully, but the non-templated one gives compilation error. Why?
template <typename ParamType>
void Template_universal_ref(ParamType&& param)
{
cout << "Template_universal_ref, param : " << param << endl;
}
void NonTemplate_universal_ref(int&& param)
{
cout << "NonTemplate_universal_ref, param : " << param << endl;
}
int main()
{
int x=5;
Template_universal_ref(x);
return 0
} In my view
NonTemplate_universal_ref(x); is special version of
Template_universal_ref(x); . Why does NonTemplate_universal_ref(x); result in compilation error?
|
|
|
|
|
The double ampersand in the non-template version is illegal. Change it to a single one for a reference parameter, or none at all for a direct value.
|
|
|
|