struct node in queue structure in c++ - c++

suppose I got a node like this:
struct node{
int a;
int b;
}
Now I want to make a queue structure in cpp. If the data type was int then we could do that easily in this way:
queue<int> a;
And we could also push or pop elements like this: a. push_back(12) or a. pop(12)
But in the earlier case when out data type is user defined how can we make such a queue and push or pop elemeqnts from it?

There is absolutely no problem in holding non-PODs like struct or class inside container like queue.
struct s1{
int a;
string b;
};
class Foo{
int a;
string b;
};
int main() {
queue<int> qi;
queue<s1> qs;
queue<Foo> qfoo;
return 0;
}

// Example program
#include <iostream>
#include <queue>
using namespace std;
struct s1{
int a;
string b;
};
class Foo{
public:
int a;
string b;
};
int main()
{
queue<Foo> q;
Foo obj;
obj.a=2;
obj.b="Object";
q.push(obj);
Foo p=q.back();
cout<<p.a<<endl;
cout<<p.b<<endl;
return 0;
}

If you have
struct node{
int a;
int b;
};
std::queue<node> q;
you can use push as follows:
q.push({ 11, 12 });
this adds a node with a = 11 and b = 12.
This works as long as copy-list-initialization is possible, as it is in this case. Otherwise you would have to use q.push(node{ 11, 12 });

Use queue a; make objects of node type and use functions a.push_back()
a.pop_front() to push and pop respectively.Also don't forget to #include queue.

Related

Initializing Structs in a Vector

How would I go about initializing a vector of struct instances in C++? For example, if I had:
struct node {int a; int b;};
int main() {
std::vector<node> V {node node1, node node2...};
}
How can I go about initializing the nodes in the vector in the declaration? Is this possible? I am using C++11 with initializer lists but am confused as to how they work, if they can be applied here.
struct node {int a; int b;};
int main() {
std::vector<node> V {{1,2}, {3,4}, ...};
}
#include <vector>
using namespace std;
struct node {int a;};
int main() {
std::vector<node> V {node{1}, node{2}};
}

Filling a List with struct

I'm new to C++/CLI and are having a hard time with Lists.
I have a structure
#using namespace System::Collections::Generic
struct myStruct {
unsigned int A ;
int B; };
and i want to create a list with mystructs
List<myStruct> myList;
But that seems not to work, Visual Studio says
"myStruct is not a valid generic Argument", but why is that so?
And how can i make this structure a "valid generic argument"?
#include <List>
struct myStruct {
unsigned int A ;
int B;
};
std::list<myStruct> myList;
int main(void) {
return 0;
}

Structure Push back not working

#include<cstdio>
#include<vector>
using namespace std;
struct Edge
{
int from;
int to;
int weight;
};
int main()
{
vector<Edge> v;
v.push_back(Edge (1,2,10));
v.push_back(Edge (2,3,30));
v.push_back(Edge (1,3,20));
return 0;
}
why push_back function is not working in vector and giving errors? how do I directly insert values in vector and not define a variable of type Edge?
Along with the answers provided in the comments, you could simply add a constructor inside your struct.
Edge(int f, int t, int w) {
from = f;
to = t;
weight = w;
}

Initialization by constructor in c++

I have question about constructor,
why following code works correctly:
#include <iostream>
using namespace std;
class mycl
{
private:
int a;
//struct
//{
char b,c;
//} ms;
public:
mycl (int _a,char _b,char _c):a (_a), b (_b), c (_c){}
};
int main() {
// your code goes here
mycl slc (15, 'a', 'f');
return 0;
}
https://ideone.com/wBgM1b
but there is a compilation error in this one
https://ideone.com/Yqxvzk
is it possible to initialize members of complex types this way?
p.s.
thank for translate and for answer.
sorry about wrong language
You want:
mycl(int _a, char _b, char _c) : a(_a), ms{_b, _c} {}
// ^^^^^^^^^^

Updating vector by passing by reference in constructor

I am trying to write a piece of code where I am passing the vector by reference through constructor of a class and updating the vector in the member function of the class. But when I get back to the main function, no update occurs in the vector:
// Header file
class A{
private:
std::vector<T> &x;
public:
A(std::vector<T>& x_):x(x_) {}
int func();
};
// Cpp file
int A::func() {
// process done
T temp;
x.push_back(temp);
}
// Main function
int main() {
std::vector<T> vec;
A a(vec);
a.func();
}
I have tried changing the vector to be a pointer in the class instead of a reference but the vector doesnt update after the function runs. Any suggestions on what to change in the program?
#include <iostream>
#include <vector>
using namespace std;
class A
{
std::vector<int> &x;
public:
A(std::vector<int>& x_):x(x_) {}
int func();
};
int A::func(){
int temp=0;
x.push_back(temp);
return 0;
}
int main(){
std::vector<int> vec;
A a(vec);
a.func();
return 0;
}
everything is ok vec changed. I think you have another question or bug that you even dont aware of .