когда я пытаюсь запустить свою программу, которая успешно компилируется, я сталкиваюсь с этой ошибкой
ag715@capa:~$ ./RAT C Parts.txt Output-file
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 18446744073709551615) >= this->size() (which is 0)
Aborted (core dumped)
в ходе исследований я считаю, что один из моих векторов может быть пустым. Вот как я добавляю элементы к моим векторам. Я выделил каждую из моих функций readfile, и все данные успешно вменяются в векторы. Я не уверен в происхождении этого вопроса, поэтому для аргументов я предоставлю весь свой код, поскольку не могу даже выделить проблему
MAIN
#include <iostream>
#include "driver.h"
using namespace std;
int main(int argc,char* argv[]){
Customer c;
Part p;
Builder b;
const string partfileName = "Parts.txt";
std::vector<Builder> Builder = readbuilderFile(argv[3]);
std::vector<Part> Parts = readpartFile(argv[2]);
std::vector<Customer> Customer = readcustomerFile(argv[1]);
fexists(partfileName);
writeFile(buildAttempt(b,variability(Customer,Builder),robotComplexity(c, Parts), Parts));
return 0;
}
HEADER
#ifndef DRIVER_H
#define DRIVER_H
#include <string>
#include <vector>
struct Customer {
std::string customerName;
std::string projectName;
std::string listofParts;
};
struct Part {
char partCode;
std::string partName;
int maximum;
int minimum;
int complexity;
};
struct Builder {
std::string builderName;
int ability;
int variability;
};
#endif
bool fexists(const std::string filename);
std::vector<Part> readpartFile(std::string a);
std::vector<Customer> readcustomerFile(std::string b);
std::vector<Builder> readbuilderFile(std::string c);
double robotComplexity(Customer c, std::vector<Part>& parts);
double variability(std::vector<Customer> customerList, std::vector<Builder> builderList);
std::vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot,std::vector<Part>& parts);
void writeFile(std::vector<double> build);
РЕАЛИЗАЦИЯ
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include "driver.h"
#include <time.h>
#include <random>
#include <vector>
using namespace std;
ofstream outputFile;
std::vector<Part> readpartFile(string fileName) //function to read Builders, Customers and Parts text file
{
std::vector<Part> parts;
std::ifstream partsList(fileName);
std::string line;
while (std::getline(partsList, line))
{
line.pop_back(); //removing '.' at end of line
std::string token;
std::istringstream ss(line);
Part part;
std::getline(ss, token, ':');
part.partCode = token[0];
std::getline(ss, part.partName, ':');
std::getline(ss, token, ':');
part.minimum = std::stoi(token);
std::getline(ss, token, ':');
part.maximum = std::stoi(token);
std::getline(ss, token, ':');
part.complexity = std::stoi(token);
parts.push_back(std::move(part));
}
return parts;
}
bool fexists(const string filename)
{
ifstream ifile(filename);
return ifile.good();
}
std::vector<Customer> readcustomerFile(string fileName)
{
std::ifstream customerList(fileName);
std::vector<Customer> Customers;
std::string line;
while (std::getline(customerList, line))
{
line.pop_back(); //removing '.' at end of line
std::string token;
std::istringstream ss(line);
Customer Customer;
std::getline(ss, token, ':');
Customer.customerName = token;
std::getline(ss, Customer.projectName,':');
std::getline(ss, token, ':');
Customer.listofParts = token;
Customers.push_back(std::move(Customer));
}
return Customers;
}
std::vector<Builder> readbuilderFile(string fileName)
{
std::vector<Builder> Builders;
std::string line;
std::ifstream buildersList(fileName);
while (std::getline(buildersList, line))
{
line.pop_back(); //removing '.' at end of line
std::string token;
std::istringstream ss(line);
Builder Builder;
std::getline(ss, token, ':');
Builder.builderName= token;
if(Builder.ability >= 0 || Builder.ability <= 99) { //error checking if an integer beteen 0 and 99 inclusive
std::getline(ss, token,':');
Builder.ability = stoi(token);
} else {
cout << "ERROR: Abilty must be an integer between 0 and 99!" << endl;
}
if(Builder.variability >= 0 || Builder.variability <= 10) { //error checking if an integer beteen 0 and 10 inclusive
std::getline(ss, token, ':');
Builder.variability = stoi(token);
} else {
cout << "ERROR: variability must be an integer between 0 and 10!" << endl;
}
Builders.push_back(std::move(Builder));
}
return Builders;
}
double robotComplexity(Customer c, std::vector<Part>& parts)
{
double total = 20;
for (Part p: parts)
{
if (c.listofParts.length() > 10)
{
cout << "Parts list cannot be greater than 10!" << endl;
} else {
total += (std::count(c.listofParts.begin(), c.listofParts.end(), p.partCode)
* p.complexity);
if (total >= 100) {
total =100;
}
}
}
return total;
}
double variability(vector<Customer> customerList, const vector<Builder> builderList) {
float partCount;
double variaiblity;
std::random_device random_device;
std::mt19937 engine{random_device()};
mt19937::result_type seed = time(0);
std::uniform_int_distribution<int> dist(0,builderList.size()), mt19937(seed);
for(Customer cus: customerList) {
for (int i = 0; i < customerList.size(); i++)
{
partCount = cus.listofParts.length();
int random_index = dist(engine);
for(Builder b: builderList)
{
if(b.builderName == builderList[random_index].builderName) {
variaiblity = 5 + partCount + b.variability;
}
}
}
}
return variaiblity;
}
vector<double>buildAttempt(Builder b, double variaiblity, double complexityRobot, vector<Part>& vP) {
vector<double> results;
std::default_random_engine generator;
std::normal_distribution<double> distribution(b.ability, variaiblity);
int sample= distribution(generator);
int attemptNumber;
Customer c;
auto partsVec = vP;
if(sample >= complexityRobot) {
cout << "Build not failed. ";
attemptNumber = 1;
cout << "Number of build attempts: " << attemptNumber;
results.push_back(sample);
results.push_back(attemptNumber);
} else {
cout << "Build failed ";
sample= distribution(generator) + 5;
robotComplexity(c, partsVec);
if(sample >= complexityRobot) {
cout << "Build not failed ";
attemptNumber = 2;
cout << "Number of build attempts: " << attemptNumber;
results.push_back(sample);
results.push_back(attemptNumber);
} else {
cout << "Build failed ";
sample= distribution(generator) + 10;
robotComplexity(c, partsVec);
if(sample >= complexityRobot) {
cout << "Too many attempts. Moving onto next customer";
attemptNumber = 3;
cout << "Number of build attempts: " << attemptNumber;
results.push_back(sample);
results.push_back(attemptNumber);
}
}
}
return results;
}
void writeFile(vector<double> build) {
ofstream outputFile("output.txt");
vector<double> robotBuild = build;
outputFile << "The number of build attempts: " << robotBuild.at( robotBuild.size()-1) << endl;
outputFile << "The random number produced: " << robotBuild.at(0) << endl;
}