I am getting a segmentation fault error after inserting an input file in this program I can't find where is the problem, could anyone help me with this?(it's the solution for the USACO training milk2 question).
#include <algorithm>
#include <bitset>
#include <limits>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
std::ifstream in ("milk2.in");
std::ofstream out ("milk2.out");
std::vector< pair <int,int> > v;
int n,i,maxn,maxs,t,ts;
in >> n;
for (i = 0; i < n; i++)
{
in >> v[i].first >> v[i].second;
}
for (i = 0; i < n; i++)
{
if (v[i].second<(v[i+1].first)
{
t=v[i+1].first-v[i].second;
if (t>maxn){
maxn=t;
}
}
else
{
ts=v[i+1].second-v[i].first;
if (ts>maxs)
{
maxs=ts;
}
}
}
out << maxs <<" "<< maxn;
return 0;
}
When you create the vector v, it's empty. So any index, even zero, will be illegal.
You need to first create the entries in the vector, e.g. by doing push_back.
Related
I have a segmentation fault on this line in c++ :
vector<TemplateElement*> children = getChildren();
Class That inherits from abstract class TemplateElement
How to fix that ?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include "strings.h"
#include "aimlthat.h"
using namespace std;
using namespace aiml;
vector<string> That::elements() {
vector<TemplateElement*> children = getChildren();
vector<string> elements;
for (int i = 0, n = children.size(); i < n; i++) {
string text = children[i]->toString();
text = trim(text);
vector<string> vsText = split(text);
for(int j=0, m=vsText.size(); j<m; ++j) {
elements.push_back(vsText[j]);
}
}
return elements;
}
/****************************************************************************/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include "strings.h"
#include "aimltemplateelement.h"
#include "aimltext.h"
using namespace std;
using namespace aiml;
vector<TemplateElement*> TemplateElement::getChildren() {
return m_vtChildren;
}
/***************************************************************************/
#ifndef __AIMLTEMPLATEELEMENT_H__
#define __AIMLTEMPLATEELEMENT_H__
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include "strings.h"
#include "aimlelement.h"
using namespace std;
using namespace aiml;
namespace aiml {
class TemplateElement : public AIMLElement {
public:
TemplateElement() {}
TemplateElement(vector<TemplateElement*> elements);
vector<TemplateElement*> getChildren();
virtual string toString() = 0;
private:
vector<TemplateElement*> m_vtChildren;
};
}
#endif
/***********************************************************************/
Here is the class That :
#ifndef __AIMLTHAT_H__
#define __AIMLTHAT_H__
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "aimltemplateelement.h"
using namespace std;
namespace aiml {
class That : public TemplateElement {
public:
That() {}
vector<string> elements();
private:
};
}
#endif
You need to check for null pointer:
for (int i = 0, n = children.size(); i < n; i++) {
if(children[i]) //Be sure that children[i] is not null before dereferencing it
{
string text = children[i]->toString();
text = trim(text);
vector<string> vsText = split(text);
for(int j=0, m=vsText.size(); j<m; ++j) {
elements.push_back(vsText[j]);
}
}
}
[EDIT]
From your comment I see that all you need is initialize the pointer to That before using it, and btw don't forget to delete it when you finish:
That* that = new That();
that->elements();
So I'm trying to call on my list of Edge*s called edgelist. I have a graph.cpp below that is supposed to display an adjacency list of the graph.
#include <sstream>
#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include "Graph.hpp"
Graph::Graph(){}
void Graph::displayGraph(){
for(int i = 0; i < vertices[vertices.size()-1].label; i++){
cout << vertices[i].label << ": ";
for(int j = 0; j <= edgeList.size(); j++){
if(edgeList[j].start==i){
cout << edgeList[j].end;
}
}
}
}
Graph.hpp includes Vertex.hpp which is below.
#ifndef Vertex_hpp
#define Vertex_hpp
#include <stdio.h>
#include <list>
#include <string>
#include <vector>
#include "Edge.hpp"
using namespace std;
class Vertex {
public:
// the label of this vertex
int label;
// using a linked-list to manage its edges which offers O(c) insertion
list<Edge*> edgeList;
// init your vertex here
Vertex(int label);
// connect this vertex to a specific vertex (adding edge)
void connectTo(int end);
};
#endif /* Vertex_hpp */
Yet, when I run my code I get an error saying that edgeList is not declared in this scope.
In Graph::displayGraph(), you are iterating on a list of Vertexs. To access the edgeList field from an object, you need to reference it as such. See the following code:
void Graph::displayGraph(){
for(int i = 0; i < vertices[vertices.size()-1].label; i++){
cout << vertices[i].label << ": ";
for(int j = 0; j <= vertices[i].edgeList.size(); j++){
if(vertices[i].edgeList[j].start==i){
cout << vertices[i].edgeList[j].end;
}
}
}
}
Tell me how to create different strings in one pointer string like array.
see following two program. 1st one give an errors. what is wrong here?
Kindly correct it.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string *j={"nilesh",
"rohit",
"samir",};
cout<<j<<endl;
}
#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
Write simply
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s[] = { "nilesh", "rohit", "samir", };
for ( const string &t : s ) cout << t << endl;
}
Also instead of the array you could use standard class std::vector<std::string>
For example
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> v = { "nilesh", "rohit", "samir", };
for ( const std::string &s : v ) std::cout << s << std::endl;
}
Why not try it in this way ?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string j[]={"nilesh",
"rohit",
"samir"};
cout<<j<<endl;
}
Printing j directly wont print all the three names. You need to print j[0], j[1] ...
I am trying to learn how Boost UUID works. I have made the following code
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace uuids;
int main() {
for (int i = 0; i < 40; ++i){
uuid uuid = random_generator()();
cout <<i+1<<" "<<"\t"<<uuid << endl;
}
system("pause");
return 0;
}
In this code i generate 40 lines with UUID codes. I want to place these lines in a function and call them by my main class int main()
Any ideas how to do this?
Generate them into a standard library container, such as e.g. std::vector<uuid>:
std::vector<uuid> foo() {
std::vector<uuid> r;
std::generate_n(back_inserter(r), 40, random_generator());
return r;
}
Live On Coliru
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace uuids;
std::vector<uuid> foo() {
std::vector<uuid> r;
std::generate_n(back_inserter(r), 40, random_generator());
return r;
}
int main() {
for(auto& uuid : foo())
cout << uuid << endl;
}
This is my code
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <set>
#include <struct.h>
#include <vector>
#include <functional>
using namespace std;
struct points {
int x,y;
};
bool operator<(const points &p1, const points &p2) {
return p1.x<p2.x;
};
vector<points> a(1000000);
int i,n,closest;
int main() {
scanf("%d\n",&n);
for (i=0; i<n-1; i++) {
scanf("%d %d\n",&a[i].x,&a[i].y);
}
sort(0,n-1,a);
return 0;
}
The errors that I get mostly state "Indirection requires pointer operand ('int' invalid). What could be wrong? I'm trying to sort the structs within the vector. I have used operator overload.
That's not how you use std::sort! Replace:
sort(0, n-1, a);
with:
sort(a.begin(), a.end());
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
struct points {
int x,y;
};
bool operator<(const points &p1, const points &p2) {
return p1.x<p2.x;
};
vector<points> a;
int i,n,closest;
int main() {
cin >> n;
for (i=0; i<n; i++) {
points p;
cin >> p.x >> p.y;
a.push_back(p);
}
sort(a.begin(), a.end());
for(i=0; i<n; i++)
cout << a[i].x << ',' << a[i].y << endl;
return 0;
}