Initializing Structs in a Vector - c++

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

Related

Create comparator for inserting triplets in min priority queue

I have made a triplet using a class with all members as integers. I want to insert the triplet in min priority queue using STL in C++. I heard that it can be done using a bool comparator function, but don't have any idea about how to use it with 3 elements.
Note: I don't want to use vector pairs for inserting 3 values (I know how to do it ), I only want to use class triplets.
Can someone help me in implementing it?
using namespace std;
#include<bits/stdc++.h>
class triplet{
public:
int element;
int arrIndex;
int elementIndex;
};
priority_queue<triplet, vector<triplet>, greater<triplet>> pq;
I don`t know why you did std::vector, std::greater but.
#include <queue>
#include <vector>
class triplet {
public:
int element;
int arrIndex;
int elementIndex;
constexpr bool operator>(const triplet& r)
{
return element > r.element;
}
};
int main()
{
std::priority_queue<triplet, std::vector<triplet>, std::greater<>> queue;
triplet a, b;
a.element = 3;
b.element = 5;
queue.push(a);
queue.push(b);
}
This is possible by define a triplet operator.
or
#include <queue>
#include <vector>
class triplet {
public:
int element;
int arrIndex;
int elementIndex;
};
template <>
struct std::greater<triplet>
{
bool operator()(const triplet& l, const triplet& r)
{
return l.element > r.element;
}
};
int main()
{
std::priority_queue<triplet, std::vector<triplet>, std::greater<triplet>> queue;
triplet a, b;
a.element = 3;
b.element = 5;
queue.push(a);
queue.push(b);
}
through template specialization.

struct node in queue structure in 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.

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

I can't make this dijkstra code compile. (The Algorithm Design Manual)

This Code is a code I built from the algorithm design manual book but I can't make it compile cause I've got little experience with pointers I think that's the main reason I think I can't compile it:
And if someone can change a little bit in the djikstra to make it through heap with the current configuration.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int MAXV=1000;
const int MAXINT=99999;
typedef struct{
int y;
int weight;
struct edgenode *next;
}edgenode;
typedef struct{
edgenode *edges[MAXV+1];
int degree[MAXV+1];
int nvertices;
int nedges;
bool directed;
}graph;
void add_edge(graph *g,int x,int y,int weight,bool directed);
void read_graph(graph *g,bool directed){
int x,y,weight,m;
g->nvertices=0;
g->nedges=0;
g->directed=directed;
for(int i=1;i<MAXV;++i) g->degree[i]=0;
for(int i=1;i<MAXV;++i) g->edges[i]=NULL;
scanf("%d %d",&(g->nvertices),&m);
for(int i=1;i<=m;++i){
scanf("%d %d %d",&x,&y,&weight);
add_edge(g,x,y,weight,directed);
}
}
void add_edge(graph *g,int x,int y,int weight,bool directed){
edgenode *p;
p=malloc(sizeof(edgenode));
p->weight=weight;
p->y=y;
p->next=g->edges[x];
g->edges[x]=p;
g->degree[x]++;
if(directed==false) add_edge(g,y,x,weight,true);
else g->nedges++;
}
int dijkstra(graph *g,int start,int end){
edgenode *p;
bool intree[MAXV+1];
int distance[MAXV+1];
for(int i=1;i<=g->nvertices;++i){
intree[i]=false;
distance[i]=MAXINT;
}
distance[start]=0;
int v=start;
while(intree[v]==false){
intree[v]=true;
p=g->edges[v];
while(p!=NULL){
int cand=p->y;
int weight=p->weight;
if(distance[cand] > distance[v]+weight) distance[cand]=distance[v]+weight;
p=p->next;
}
v=1;
int dist=MAXINT;
for(int i=1;i<=g->nvertices;++i)
if((intree[i]==false) && (dist > distance[i])){
dist=distance[i];
v=i;
}
}
return distance[end];
}
int main(){
graph g;
read_graph(&g,false);
int x=1,y,shortest;
while(x!=0){
scanf("%d %d",&x,&y);
shortest=dijkstra(&g,x,y);
printf("The shortest path from %d to %d is %d",x,y,shortest);
}
return 0;
}
Change the definition of the struct, and it would compile.
struct edgenode_tag
{
int y;
int weight;
struct edgenode_tag *next;
};
typedef edgenode_tag edgenode;
While this will solve your problem, don't trust my answer below until someone better than me comments on it.
What was wrong in your code ?
You are using the typedef-ed type before the compiler knows about that type. Instead, you need to use the structure_tag to define the member pointer of type itself.
typedef struct
{
...
my_struct* pS;
...
} my_struct; // at this point compiler will know about *my_struct* type
// Hence, you can not use that name until after this line.
// To define the member pointer of type itself you need to
// to use the struct_tag, as I did in your example.
// where, struct_tag is *edgenode_tag*
EDIT:
Also, malloc returns *void**, which you need to cast to the type you are assigning it to.
So, inside function add_edges, make this correction (please read more about this in book, it is important to understand this):
p = (edgenode*)malloc(sizeof(edgenode));
typedef struct
{
int y;
int weight;
struct edgenode *next;
} edgenode;
Here you are using a typedef struct without defining this and then you are using edgenode in your struct defination before defining edgenode.
So you should change it to:
typedef struct _edgenode
{
int y;
int weight;
struct _edgenode *next;
} edgenode;