Structure Push back not working - c++

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

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.

C++ get data from double pointer

I am new in C++ area and faced with double pointers. The question is how to set x=5 in function test?
#include <iostream>
using namespace std;
typedef struct _DoublePointer
{
int x;
int y;
} DoublePointer;
void test(_DoublePointer** pointer)
{
}
void main()
{
_DoublePointer* pointer = new _DoublePointer;
_DoublePointer** doublePointer = &pointer;
test(doublePointer);
}
the way to do that is
(*pointer)->x = 5;

how to use vector and struct members in method

im new in c++ (and not to old in programming...) and i have problem with handling vectors and strucs in class.
basically i have a vector and a array of pointers to struct members in the class and i want work on the in my methos but im doing something worng/
here is my movement.h
#pragma once
using namespace std;
class movement
{
private:
static const int MAX_ROW_PER_TRACKER = 100;
static const int MIN_TO_START_CALC = 30;
static const int MAX_TRACKERS = 20;
struct tracker
{
int id;
double a[MAX_ROW_PER_TRACKER];
double b[MAX_ROW_PER_TRACKER];
double c;
};
vector<int> trackersOrder[MAX_TRACKERS] = {};
tracker* trackersArr[MAX_TRACKERS];
public:
movement();
void addRow(int a, int b, int c);
~movement();
};
and my movement.cpp
#include "stdafx.h"
#include "movement.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
movement::movement()
{
}
void movement::addRow(int id, int a, int b)
{
int index;
vector<int>::iterator searchID = find(trackersOrder.begin(), trackersOrder.end(), ID);
if (searchID == trackersOrder.end())
{
vector<int>::iterator freeLocation = find(trackersOrder.begin(), trackersOrder.end(), 0);
index = freeLocation - trackersOrder.begin();
trackersOrder.insert(trackersOrder.begin + index, id);
structArr[index] = new tracker;
structArr[index]->id = id;
structArr[index]->a[0] = a;
structArr[index]->b[0] = b;
structArr[index]->c = 0;
}
}
movement::~movement()
{
}
so when i send to method "addRow" id, and b i want to first check if i allready have this id in my vector (the vector just give me the index for the structs array) and if not then if put the id in the first empty place in the vector and on the structs array/
but from some reasin its look to me that the methid dont reconized the vector and the structs. can you help me understand why?
p.s - i can bet that i have more mistakes in my code, its my firs try with pointers and ect. (im comming from the good life in Matlab) so i will be happy to learn on them also
thank you very much!
The main problem
The problem is that in your code, trackersOrder is not a vector but an array of vectors:
vector<int> trackersOrder[MAX_TRACKERS] = {}; // array of MAXTRACKERS vectors !!
The solution
If you define it as simple vector, it should work better:
vector<int> trackersOrder;
If you want to set its size do it in the movement constructor:
movement::movement() : trackersOrder(MAX_TRACKERS)
{
}
Other issues
There is a case typo with an ID that should be id.
auto searchID = find(trackersOrder.begin(), trackersOrder.end(), id); // by the way auto is easier + ID corrected
There are a missing () after a begin whicn transforms unfortunately your iterator arithmetic into function pointer arithmetic (sic!!):
trackersOrder.insert(trackersOrder.begin() + index, id); // corrected
Finally, there are a couple of structArr that should be replaced by trackersArr.
The result does finally compile (online demo)

error: no viable conversion from 'edge' to 'const value_type'?

I am simply trying to implement a directed weighted graph using vectors, lists, and classes in C++ however I am getting this error. The line it is giving me this error is line 42:21 where graph[u].push_back(edge(v, w));
#include <iostream>
#include <vector>
#include <list>
#include <stack>
using namespace std;
class edge
{
private:
int cost;
int vertex;
public:
edge(int vertex, int cost)
{
this->vertex = vertex;
this->cost = cost;
}
int getVertex() const
{
return vertex;
}
int getCost() const
{
return cost;
}
};
class Graph
{
private:
int V;
std::vector<std::list<edge> > *graph;
public:
Graph(int V);
void addEdge(int u, int v, int w);
void DFS();
};
Graph::Graph(int V)
{
this->V = V;
graph = new std::vector<std::list<edge> >(V);
}
void Graph::addEdge(int u, int v, int w)
{
graph[u].push_back(edge(v, w));
}
int main()
{
return 0;
}
Thank you for your help!
graph is a pointer to a vector, not a vector, so you want:
(*graph)[u].push_back(edge(v, w));
But it doesn't seem to need to be a pointer. Wouldn't it be simpler as just a plain vector? It would fix your memory leak too.

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;