vector subcript out of range c++ - vector

I'm trying to write a set (in mathematical sens) in C++ but I have problem. When i launch my program i have "vector subcript out of range" error. I'm using Visual Studio and its not show any error. Sorry for my bad language.
#include "Set.h"
template <class T>
void Set<T>::print() {
cout << endl << "{";
for(int i = 0; i < set.size() - 1; i++) {
cout << set[i] << ", ";
}
//cout << set[set.size()] << "}" << endl;
cout << endl;
}
template <class T>
bool Set<T>::contains(T value) {
for(unsigned int i = 0; i < set.size(); i++) {
if(set[i] == value) return true;
}
return false;
}
template <class T>
void Set<T>::operator +(const T &obj) {
if(!contains(obj)) set.push_back(obj);
}
template <class T>
void Set<T>::operator -(const T &obj) {
if(contains(obj)) {
unsigned int i = 0;
// NIE DZIAƁA
while(i < set.size()) {
if(set[i] == obj) break;
i++;
}
while(i < (set.size() - 1)) {
set[i] = set[i + 1];
i++;
}
//set[i] = set[set.size()];
}
}
template <class T>
Set<T> Set<T>::operator +(const Set<T> &obj) {
Set<T> result;
for(unsigned int i = 0; i < set.size(); i++) {
result + set[i];
}
for(unsigned int i = 0; i < obj.set.size(); i++) {
if(!result.contains(obj.set[i])) {
result + obj.set[i];
}
}
return result;
}
template <class T>
Set<T> Set<T>::operator -(const Set<T> &obj) {
Set<T> result;
for(unsigned int i = 0; i < set.size() - 1; i++) {
if(!contains(obj.set[i])) {
result + set[i];
}
}
return result;
}
template <class T>
Set<T> Set<T>::operator *(const Set<T> &obj) {
Set<T> result;
for(unsigned int i = 0; i < set.size() - 1; i++) {
if(contains(obj.set[i])) {
result + set[i];
}
}
return result;
}
Can someone help me?

I fix my problem. It did in print method. It's correct version of this method:
template <class T>
void Set<T>::print() {
if(!set.empty()) {
cout << endl << "{";
for(unsigned int i = 0; i < set.size() - 1; i++) {
cout << set[i] << ", ";
}
cout << set[set.size() - 1] << "}" << endl;
cout << endl; }
}
Thanks for help!

Related

I am trying to print Reverse of a Sequence of Numbers using Stack. Stack is implemented using Vector. But I am getting Segmentation Fault

Can you please help me find the error in printing the reverse of sequence using stacks implemented by vector?
I am getting a Segmenattion fault
#include <iostream>
#include<vector>
using namespace std;
class stack{
public :
int top;
vector<int> data;
bool isempty(){return top == -1;}
void push(int x){data[++top] = x;}
void pop(){--top;}
int topper(){return data[top];}
};
int main()
{
stack s;
int n;
s.top = -1;
cout << "enter the number of integers" << endl;
cin >> n;
for(int i =0; i < n; i ++){
s.push(i);
}
while(!s.isempty()){
cout << s.topper();
s.pop();
}
return 0;
}
This problem occurs, because a vector has size = 0 by default.
You can either resize the vector, before you add values into it like so:
#include <iostream>
#include<vector>
using namespace std;
class stack {
public:
int top;
vector<int> data;
bool isempty() { return top == -1; }
void push(int x) { data.resize(++top+1); data[top] = x; }
void pop() { --top; }
int topper() { return data[top]; }
};
int main()
{
stack s;
int n;
s.top = -1;
cout << "enter the number of integers" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
s.push(i);
}
while (!s.isempty()) {
cout << s.topper();
s.pop();
}
return 0;
}
Or you can use the built-in functionality for vectors like that, which I think is the far better solution:
#include <iostream>
#include<vector>
using namespace std;
class stack {
public:
vector<int> data;
bool isempty() { return data.size() == 0; }
void push(int x) { data.push_back(x); }
void pop() { data.pop_back(); }
int topper() { return data.back(); }
};
int main()
{
stack s = stack();
int n;
cout << "enter the number of integers" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
s.push(i);
}
while (!s.isempty()) {
cout << s.topper();
s.pop();
}
return 0;
}

Segmentation fault inside range

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue> // std::priority_queue
using std::vector;
using std::cin;
using std::cout;
struct fj{
int indexI=0;
int freeT=0;
};
struct DereferenceCompareNode : public std::binary_function<fj, fj, bool>
{
bool operator()(const fj lhs, const fj rhs) const
{
return lhs.freeT > rhs.freeT;
}
};
class JobQueue {
private:
int num_workers_;
vector<int> jobs_;
vector<int> assigned_workers_;
vector<long long> start_times_;
void WriteResponse() const {
for (int i = 0; i < jobs_.size(); ++i) {
cout << assigned_workers_[i] << " " << start_times_[i] << "\n";
}
}
void ReadData() {
int m;
cin >> num_workers_ >> m;
jobs_.resize(m);
std::cout<<"Read fault"<<"\n";
for(int i = 0; i < m; i++)
cin >> jobs_[i];
std::cout<<"Read fault ends"<<"\n";
}
void AssignJobs() {
// TODO: replace this code with a faster algorithm.
std::cout<<"Fault point 1"<<"\n";
assigned_workers_.resize(jobs_.size());
start_times_.resize(jobs_.size());
vector<long long> next_free_time(num_workers_, 0);
std::priority_queue<int, vector<int>, std::greater<int> > thread;
std::priority_queue<fj, vector<fj>, DereferenceCompareNode > freeJob;
/*
for (int i = 0; i < jobs_.size(); ++i) {
int duration = jobs_[i];
int next_worker = 0;
for (int j = 0; j < num_workers_; ++j) {
if (next_free_time[j] < next_free_time[next_worker])
next_worker = j;
}
assigned_workers_[i] = next_worker;
start_times_[i] = next_free_time[next_worker];
next_free_time[next_worker] += duration;
}
*/
std::cout<<"dump point 2"<<"\n";
for(int i=0;i<num_workers_;i++){
thread.push(i);
}
std::cout<<"dump point 1"<<"\n";
int counter = 0;
while(jobs_.size()!=0){
std::cout<<"jobs_.size:"<<jobs_.size()<<"\n";
std::cout<<"freeJob.size:"<<freeJob.size()<<"\n";
//check logic
do{
if(freeJob.top().freeT == counter){
std::cout<<"freeJob.top().freeT:"<<freeJob.top().freeT<<"\n";
std::cout<<"counter:"<<counter<<"\n";
thread.push(freeJob.top().indexI);
freeJob.pop();
}else{
break;
}
}
while(freeJob.size()!=0);
std::cout<<"Thread:"<<thread.size()<<"\n";
while(thread.size()!=0){
if(jobs_.size()!=0){
fj currA;
currA.indexI = thread.top();
currA.freeT = jobs_.at(0)+counter;
std::cout<<"currA.indexI:"<<currA.indexI<<"\n";
std::cout<<"currA.freeT:"<<currA.freeT<<"\n";
thread.pop();
jobs_.erase(jobs_.begin());
assigned_workers_.push_back(currA.indexI);
start_times_.push_back(currA.freeT);
}else{
break;
}
}
counter++;
}
}
public:
void Solve() {
ReadData();
AssignJobs();
WriteResponse();
}
};
int main() {
std::ios_base::sync_with_stdio(false);
JobQueue job_queue;
job_queue.Solve();
return 0;
}
I am getting segmentation fault in function ReadData while taking inputs for vector jobs.
I am getting fault even when I am inside bounds of defined size.
Everything was fine when have not written AssignJob function.
Am I doing something wrong with some bounds or taking illegal inputs format or messing with some other stuff?
Am I doing something wrong
Yes, you are: freeJob starts out empty, so this is undefined behavior:
if(freeJob.top().freeT == counter){
In fact, you never push anything into freeJob, you only pop() things from it.

Expected unqualified-id before '.' token

I'm pretty new at c++ so talk caveman to me. I'm trying to get the code to run in a loop and and when the loop is done calculate the total and everything ordered. I'm running into this error and I'm not sure why.
[Error] expected unqualified-id before '.' token
#include <iostream>
#include<string>
#include <vector>
using namespace std;
string again;
char pType, pSize, topping, temp;
const int SMALL = 1;
int type = 0, size = 0;
const int MEDIUM = 2;
const int LARGE = 3;
const int DEEPDISH = 1;
const int HANDTOSSED = 2;
const int PAN = 3;
double total = 0;
class Pizza
{
private:
int type;
int size;
bool cheese;
bool pepperoni;
public:
Pizza();
int getType();
int getSize();
bool getCheese();
bool getPepperoni();
void setType(int t);
void setSize(int s);
void setCheese(bool choice);
void setPepperoni(bool choice);
void outputDescription();
double computePrice();
};
class Order
{
private:
vector<Pizza> c;
public:
Order();
void customerOrder();
void customerTotal();
void customerinput();
};
Pizza::Pizza()
{
type = DEEPDISH;
size = SMALL;
cheese = pepperoni = false;
}
int Pizza::getType()
{
return type;
}
int Pizza::getSize()
{
return size;
}
bool Pizza::getCheese()
{
return cheese;
}
bool Pizza::getPepperoni()
{
return pepperoni;
}
void Pizza::setType(int t)
{
type = t;
}
void Pizza::setSize(int s)
{
size = s;
}
void Pizza::setCheese(bool choice)
{
cheese = choice;
}
void Pizza::setPepperoni(bool choice)
{
pepperoni= choice;
}
void Pizza::outputDescription()
{
switch (size)
{
case SMALL:
cout << "Small "; break;
case MEDIUM:
cout << "Medium "; break;
case LARGE:
cout << "Large "; break;
default:
cout << "Unknown" ;
}
switch (type)
{
case DEEPDISH:
cout << "deepdish "; break;
case HANDTOSSED:
cout << "hand tossed "; break;
case PAN:
cout << "pan "; break;
default:
cout << "Unknown";
}
cout << "pizza";
}
double Pizza::computePrice()
{
double cost = 0.0;
switch (size)
{
case SMALL:
cost += 10; break;
case MEDIUM:
cost += 14; break;
case LARGE:
cost += 17; break;
}
if (cheese)
cost += 2.0;
if (pepperoni)
cost += 2.0;
return cost;
}
Order custmizedTotal;
Pizza myPizza;
bool done=false;
void Order::customerinput(){
while ( again == "y"){
cout << "What sized pizza, please enter S, M OR L: ";
cin >> pSize;
cin.clear();
switch(pSize)
{
case 'S': case 's':
size = SMALL; break;
case 'M': case 'm':
size = MEDIUM; break;
case 'L': case 'l':
size = LARGE; break;
}
cout << "What type of pizza, enter D for Deepdish, H for Hand tossed, and P for Pan: ";
cin >> pType;
cin.clear();
switch(pType)
{
case 'D': case 'd':
type = DEEPDISH; break;
case 'H': case 'h':
type = HANDTOSSED; break;
case 'P': case 'p':
type = PAN; break;
}
myPizza.setSize(size);
myPizza.setType(type);
cout << "Would you like cheese (y/n)? ";
cin >> topping;
cin.clear();
if (topping == 'Y' || topping == 'y')
myPizza.setCheese(true);
cout << "Would you like pepperoni (y/n)? ";
cin >> topping;
cin.clear();
if (topping == 'Y' || topping == 'y')
myPizza.setPepperoni(true);
cout << endl
<< "Your order: ";
myPizza.outputDescription();
cout << endl;
cout << "Price: $" << myPizza.computePrice() << endl;
cout << "Again? (y/n)";
cin >> again;
}
}
void Order::customerTotal(){
cout << "Your Total order is: " << endl;
for(int i=0; i<c.size(); i++)
{
c[i].outputDescription();
cout << endl;
cout << c[i].computePrice();
cout << endl;
total=total+c[i].computePrice();
}
cout << "Totat Cost: $" << total;
cout << endl;
c.push_back(myPizza);
}
int main()
{
custmizedTotal.customerinput();
//Order.customerinput();
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
Replace
int main(){
Order.customerinput(); //error is here
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
By:
int main(){
custmizedTotal.customerinput(); // Change this line
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
The second error that you have as caused because you forgot to define Order constructor.
Add this to your code (above main() method):
Order::Order(){
// Set the initial values for order
}
You also forgot to add customerOrder method (but this does not cause error since you are not using this method):
void Order::customerOrder() {
}

Constant container (map) - eliminate heap allocation

If I create a static const std::map, it will allocate memory on heap. Following code throws bad_alloc:
#include <iostream>
#include <map>
class A {
public:
static const std::map<int, int> a;
};
const std::map<int, int> A::a = { { 1, 3} , { 2, 5} };
void* operator new ( std::size_t count )
{
throw std::bad_alloc();
}
int
main (void)
{
for(auto &ai: A::a) {
std::cout << ai.first << " " << ai.second << "\n";
}
return 0;
}
Is it possible to create this constant map somehow without having memory allocation?
As Igor Tandetnik suggested, a custom allocator would do the trick. The following is a quick'n'dirty example of a simple linear allocator, which returns memory slots from a static buffer:
#include <iostream>
#include <map>
#include <cassert>
template <typename T>
class LinearAllocator {
static constexpr size_t _maxAlloc = 1<<20;
using Buffer = std::array<T, _maxAlloc>;
using FreeList = std::array<bool, _maxAlloc>;
static Buffer _buffer;
static FreeList _allocated;
public:
typedef T* pointer;
typedef T value_type;
template<typename U>
struct rebind { typedef LinearAllocator<U> other; };
pointer allocate(size_t /*n*/, const void *hint=0) {
for(size_t i = 0; i < _maxAlloc; ++i) {
if(!_allocated[i]) {
_allocated[i] = true;
return &_buffer[i];
}
}
throw std::bad_alloc();
}
void deallocate(pointer p, size_t /*n*/) {
assert(p >= &_buffer[0] && p < &_buffer[_maxAlloc]);
_allocated[p-&_buffer[0]] = false;
}
LinearAllocator() throw() { }
LinearAllocator(const LinearAllocator &a) throw() { }
template <class U>
LinearAllocator(const LinearAllocator<U> &a) throw() { }
~LinearAllocator() throw() { }
};
template <typename T>
typename LinearAllocator<T>::Buffer LinearAllocator<T>::_buffer;
template <typename T>
typename LinearAllocator<T>::FreeList LinearAllocator<T>::_allocated;
using MyMap = std::map<int, int, std::less<int>,
LinearAllocator<std::pair<int,int> > >;
// make sure we notice if new gets called
void* operator new(size_t size) {
std::cout << "new called" << std::endl;
}
int main() {
MyMap m;
m[0] = 1; m[1] = 3; m[2] = 8;
for(auto & p : m)
std::cout << p.first << ": " << p.second << std::endl;
return 0;
}
Output:
0: 1
1: 3
2: 8
Note that this allocator will only handle requests for single slots at a time. I'm sure you will figure out how to extend it according to your requirements.

How to optimize this Qt code (QByteArray conversion)?

I need to perform some regexp operations on binary data. I wrote a function to convert QByteArray data in a hexa string representation. Each byte is prepended by 'x' for parsing purpose.
How could this code be optimized?
QByteArray data;
QByteArray newData;
for (int i = 0; i < data.size(); i++) {
QString hex;
hex.setNum(data[i], 16);
if (data[i] < 10) {
hex.prepend("x0");
} else {
hex.prepend("x");
}
newData.append(hex.toLatin1());
}
The code you posted has two bugs in it that I corrected.
1) Assuming you always want two hex digits you want to check if the value is less than 16, not 10.
2) QString::setNum has no overload for char, so the value is promoted to a larger type. For a value like 128, which is negative in a signed char, you would get x0ffffffffffffff80 due to sign extension.
The function foo1 is your original code with the bugs fixed, and foo2 is a more optimal version that avoids creating a temporary QString since the conversion to unicode and back isn't free, and prepending values to a string requires additional copying.
I used QElapsedTimer because on Windows where I am testing it uses the high resolution PerformanceCounter clock. If you are on another platform it might be less accurate. You can see the different types of clocks it may use in the documentation.
Set display_converted_string to true if you want the converted string printed to verify they are identical.
#include <QString>
#include <QByteArray>
#include <QElapsedTimer>
#include <iostream>
QByteArray foo1(QByteArray data)
{
QByteArray newData;
for (int i = 0; i < data.size(); i++) {
unsigned char c = data[i];
QString hex;
hex.setNum(c, 16);
if (c < 16) {
hex.prepend("x0");
} else {
hex.prepend("x");
}
newData.append(hex.toLatin1());
}
return newData;
}
QByteArray foo2(QByteArray data)
{
static const char digits[] = {'0','1','2','3','4','5','6','7',
'8','9','a','b','c','d','e','f'};
QByteArray newData;
newData.reserve(data.size() * 3);
for (int i = 0; i < data.size(); i++)
{
unsigned char c = data[i];
newData.append('x');
newData.append(digits[(c >> 4) & 0x0f]);
newData.append(digits[c & 0x0f]);
}
return newData;
}
int main()
{
const int iterations = 10000;
const bool display_converted_string = false;
QElapsedTimer t;
std::cout << "Using clock type " << t.clockType() << ".\n";
QByteArray data(256, 0);
QByteArray newData;
qint64 elapsed1 = 0, elapsed2 = 0;
//Set the values in data to 0-255 to make sure all values are converted properly.
for(int i = 0; i < data.size(); ++i)
{
data[i] = i;
}
t.start();
for(int i = 0; i < iterations; ++i)
{
newData = foo1(data);
}
elapsed1 = t.nsecsElapsed();
std::cout << "foo1 elapsed time = " << elapsed1 << "\n";
if(display_converted_string)
{
std::cout << "newData = " << newData.data() << "\n";
}
t.restart();
for(int i = 0; i < iterations; ++i)
{
newData = foo2(data);
}
elapsed2 = t.nsecsElapsed();
std::cout << "foo2 elapsed time = " << elapsed2 << "\n";
if(display_converted_string)
{
std::cout << "newData = " << newData.data() << "\n";
}
return 0;
}

Resources