Vector iterator not dereferencable (vector inside vector) - c++

I am trying to write some data from these vectors onto text files. When I run the code it returns a run time error. Category, Product, Cart, Customerand Address are all struct with members that each get_member returns.
ofstream write_cats;
write_cats.open("catprd.dat", ios::out, ios::trunc);
vector<Category>::iterator i;
write_cats << cats.size() << endl;
for (i = cats.begin(); i < cats.end(); i++) {
write_cats << i -> get_catid() << '\t';
}
vector<Product>::iterator j;
write_cats << prods.size() << endl;
for (j = prods.begin(); j < prods.end(); j++) {
write_cats << j -> get_prodid() << '\t';
write_cats << j -> get_prodprice() << endl;
}
write_cats.close();
ofstream write_carts;
write_carts.open("carts.dat", ios::out, ios::trunc);
vector<Cart>::iterator k;
write_carts << carts.size() << endl;
for (k = carts.begin(); k < carts.end(); k++) {
write_carts << k -> get_cartid() << '\t';
write_carts << k -> get_day() << endl;
}
vector<Cart_item>::iterator l;
write_carts << cart_items.size() << endl;
for (l = cart_items.begin(); l < cart_items.end(); l++) {
write_carts << l -> get_cartitemid() << '\t';
write_carts << l -> get_qty() << endl;
}
write_carts.close();
ofstream write_custs;
write_custs.open("custs.dat", ios::out, ios::trunc);
vector<Customer>::iterator m;
vector<Address>::iterator n;
write_custs << custs.size() << endl;
for (m = custs.begin(); m < custs.end(); m++) {
write_custs << m -> get_cust_id() << '\t';
write_custs << n -> get_zip_code() << endl;
}
write_custs.close();
Returns run time error "Vector iterator not dereferencable"
Here is how struct Address looks like:
using namespace std;
#pragma once
#include <string>
struct Address {
public:
int get_st_number() const{return st_number;}
int get_zip_code() const{return zip_code;}
string get_st_name() const{return st_name;}
Address(){}
Address (int num, string name, int zip)
: st_number(num), st_name(name), zip_code(zip) {}
private:
int st_number;
int zip_code;
string st_name;
};
and struct Customer:
struct Customer {
public:
Address get_address() const{return addr;}
int get_cust_id() const{return cust_id;} customer id
string get_name() const{return cust_name;}
Customer (int id, string n, Address a)
: cust_id(id), cust_name(n), addr(a) {}
string display_addr() const {
std::cout<<setw(15)<<cust_name<<" ";
std::cout<<setw(15)<<cust_id<<" ";
return string();
}
private:
int cust_id;
string cust_name;
Address addr;
};

you forget to initialize vector<Address>::iterator n;

You are declaring an iterator n, but not initialising it to a dereferencable value. From your update, it looks like you want to print the Address associated with the customer; so you'd access via the customer referred to by m, rather than a separate iterator:
write_custs << m -> get_cust_id() << '\t';
write_custs << m -> get_address().get_zip_code() << endl;
Also, it might be a good idea to scope each iterator inside its loop; that's less error-prone than declaring a new one in the outer scope each time:
for (vector<Whatever>::const_iterator i = stuff.begin(); i != stuff.end(); ++i) {
// do stuff with "i"
}
// "i" is no longer available - no danger of accidentally using it again.
And a couple of other points:
You should use != rather than < to compare with the end() iterator; < doesn't work for some types of iterator;
You should write a new-line to the file as '\n' rather than endl; endl flushes the file buffer, forcing the file to be written to disk at that point, which can be extremely slow.

Related

Size of unordered map keeps increasing

The size of unordered map keeps increasing in the last for loop? Why is it so?
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size() != t.size()){
return false;
}
unordered_map<char,int> sm;
unordered_map<char,int> tm;
for (int i = 0; i<s.size();i++){
sm[s[i]]++;
tm[t[i]]++;
}
for (int j = 0; j<sm.size();j++){
cout << j << endl;
cout << sm.size() <<endl;
if(sm[j] != tm[j]){
cout << sm[j] << endl;
cout << tm[j] << endl;
cout << j << endl;
return false;
}
}
return true;
}
};
I have tried to search online but can't find any relevant answer.
https://en.cppreference.com/w/cpp/container/unordered_map/operator_at
Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.
sm[j] != tm[j]
The [] operator on maps inserts entries if they didn't already exist.
You probably wanted to call .find(j) instead

C++ cout inside loop for printing vector of vectors fails

template<typename s>
void vecprint2d(const s& vec){
cout<<"{"<<endl;
for(int x = 0; x < vec.size(); x++){
cout<<"{";
for(int y = 0; y < vec[x].size() - 1;y++){
cout << vec[x][y]<<", ";
}
cout<<vec[x][vec[x].size() - 1]<<"}"<<endl;
}
cout<<"}"<<endl;
}
int main(){
vector<vector<int>> vec = {{1,2,3},{},{4,5,6}};
vecprint2d(vec);
return 0;
}
in my attempt at a function for printing a vector of vectors,
why does cout inside the inner loop cause problems, or is the problem elsewhere?
the output right now looks like:
{
{
If the inner vector's size is 0, size() - 1 will overflow and it will loop forever and/or crash. Could that be what is happening on your raspberry pi?
To avoid this, handle 0-sized vectors as well.
For example like this:
template<typename s>
void vecprint2d(const s& vec) {
cout << "{" << endl;
for (auto const& row : vec) {
cout << "{";
int i = 0;
for (auto const& val : row) {
if (i++)
cout << ", ";
cout << val;
}
cout << "}" << endl;
}
cout << "}" << endl;
}
The size() function of an empty vector will return an (unsigned) value of zero and, in your inner loop, you test y against vec[x].size() - 1. This will give a value that has 'underflowed' and thus have the maximum value that a size_t variable can hold, so the y loop will run a very large number of times! However, it will likely fail on the first loop, because trying to access any element of an empty vector is undefined behaviour.
To fix this, enclose your inner loop in an "is it empty" if block (in fact, you should do this for both loops). Here's a possible solution:
template<typename s>
void vecprint2d(const s& vec)
{
cout << "{" << endl;;
if (!vec.empty()) for (size_t x = 0; x < vec.size(); x++) {
cout << "{";
if (!vec[x].empty()) {
for (size_t y = 0; y < vec[x].size() - 1; y++) {
cout << vec[x][y] << ", ";
}
cout << vec[x][vec[x].size() - 1];
}
cout << "}" << endl;
}
cout << "}" << endl;
}
Feel free to ask for further clarification and/or explanation.

Operator Overloading solution

i have made a c++ code. An MList that holds items in it. I overloaded the << operator to print the values in MList in a particular format. Here is the code:
friend ostream& operator<<(ostream &out, const MSet<V> &m)
{
string s = "";
s += "Size " + to_string(m.size_) + "\n";//out << m.size() << endl;
s += "Cap " + to_string(m.capacity_) + "\n"; //out << m.capacity() << endl;
for (int i = 0; i < m.size_; i++)
{
if (i < m.size_ - 1)
s += m.ary[i].element + ",";//out << m.ary[i].element << ",";
else
s += m.ary[i].element;
}
//cout << "String : " << s;
return out << s;
}
But it does not print correct value. It prints the size and capacity right but not the values. Instead of values, it prints some signs like heart:
You can see it prints size and capacity right but not the values. Here is the relevant code. I am executing case 2 only right now:
#include<iostream>
using std::cout; using std::endl;
using std::ostream; using std::cin; using std::boolalpha;
#include<string>
using std::string;
using namespace std;
template <class V>
struct SetElement
{
V element;
int cnt;
SetElement() = default;
SetElement(V v) : element(v){}
};
template <class V>
ostream &operator<<(ostream & o,const SetElement<V> &p)
{
return o << p.element;
}
template <class V>
class MSet
{
private:
SetElement<V> *ary;
size_t capacity_;
size_t size_;
public:
MSet(V val)
{
capacity_ = 2;
ary = new SetElement<V>[capacity_];
ary[0].element = val;
ary[0].cnt = 1;
size_ = 1;
}
SetElement<V>* find(V val)
{
SetElement<V> *found = nullptr;
bool yes = false;
for (int i = 0; i < size_ && !yes; i++)
{
if (ary[i].element == val)
{
found = &ary[i];
yes = true;
}
}
return found;
}
friend ostream& operator<<(ostream &out, const MSet<V> &m)
{
string s = "";
s += "Size " + to_string(m.size_) + "\n";//out << m.size() << endl;
s += "Cap " + to_string(m.capacity_) + "\n"; //out << m.capacity() << endl;
for (int i = 0; i < m.size_; i++)
{
if (i < m.size_ - 1)
s += m.ary[i].element + ",";//out << m.ary[i].element << ",";
else
s += m.ary[i].element;
}
//cout << "String : " << s;
return out << s;
}
};
int main(){
int test;
long l1, l2, l3;
cin >> test;
cout << boolalpha;
switch (test){
// ...
case 2: {
cin >> l1 >> l2;
MSet<long> m_l(l1);
auto p = m_l.find(l1);
if (p != nullptr)
cout << *p << endl;
else
cout << "Val:" << l1 << " not found " << endl;
p = m_l.find(l2);
if (p != nullptr)
cout << *p << endl;
else
cout << "Val:" << l2 << " not found " << endl;
//cout << "MList \n";
cout << m_l;
break;
}
// ...
}
}
You're adding the values into a temporary string, which may involve implicit conversions depending of the template type (here your numerical values were converted into characters).
Just print the values, without the temporary string:
friend ostream& operator<<(ostream &out, const MSet<V> &m)
{
out << "Size " << m.size_ << endl;
out << "Cap " << m.capacity_ << endl;
for (int i = 0; i < m.size_; i++)
{
if (i < m.size_ - 1)
out << m.ary[i].element << ",";
else
out << m.ary[i].element;
}
return out;
}

C++ Hamming Function

This program is supposed to create three arrays of class object My_array. The first array is filled with random numbers. The second array is an exact copy of the first. The third array is entered by the user. The program checks to make sure that the first two arrays indeed equal each other and then it check to the hamming distance of the first and third array. The professor defines the hamming distance as each part off the array that is different.
My problem has been getting hamming to work. I actually have a hard time with operating overloading so I am surprised that works (well I have no errors showing in VS Studio) but not the hamming part. Any help would be appreciated. There are three files in order: main.cpp, my_array.cpp, and my_array.h. Function definitions and declarations were provided by professor. I am required to insert how each function operates.
#include "my_array.h"
#include <iostream>
using namespace std;
int main()
{
int size;
cout << "How big of an array shall we work with? ";
cin >> size;
My_array a(size);
My_array b(size);
My_array c(size);
a.randomize(100);
b = a;
c.input();
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a == b: " << (a == b) << endl;
cout << "The hamming distance is: " << a.hamming(c);
return 0;
}
#include "my_array.h"
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <time.h>
// Constructor
My_array::My_array(int the_size)
{
array = NULL;
size = 0;
resize(the_size);
}
// Destructor.
My_array::~My_array()
{
empty();
}
// Copy constructor
My_array::My_array(My_array &data)
: size(data.size)
{
array = new int[size];
for (int i = 0; i<size; i++)
array[i] = data.array[i];
}
// Overloaded assignment operator.
My_array &My_array::operator=(My_array &data)
{
if (this != &data) {
resize(data.size);
for (int i = 0; i<size; i++)
array[i] = data.array[i];
}
else
cout << "Attempt to copy an object on itself. "
<< "Operation ignored." << endl;
return *this;
}
void My_array::input()
{
int j;
cout << "Please enter " << size << " numbers.\n";
for (int i = 0; i < size; i++)
{
cout << "Number " << i + 1 << ": ";
cin >> j;
array[i] = j;
}
}
void My_array::randomize(int limit)
{
srand(time(NULL));
for (int i = 0; i < size; i++)
array[i] = rand() % limit + 1;
}
bool My_array::operator ==(My_array &data)
{
if(this->size != data.size)
return false;
for (int i = 0; i <size; i++)
{
if (*this[i].array != data.array[i])
return false;
}
return true;
}
bool My_array::operator !=(My_array &data)
{
if (*this == data)
return false;
return true;
}
int My_array::hamming(My_array &data)
{
int ham = 0;
for (int i = 0; i < size; i++)
if (*this[i].array != data[i].array)
ham++;
return ham;
}
// This function will empty the target object
void My_array::empty()
{
if (size != 0 && array != NULL) {
size = 0;
delete[] array;
}
}
// Resize the array.
void My_array::resize(int the_size)
{
if (size >= 0) {
empty();
if (the_size != 0) {
size = the_size;
array = new int[size];
}
}
else
cout << "Resize attepmted with a negative size. "
<< "Operation ignored." << endl;
}
// Access an element of the array.
int &My_array::operator[](int index)
{
if (index < size)
return array[index];
else {
cerr << "Illegal access to an element of the array." << endl
<< "The size of the array was " << size
<< " and the index was " << index << endl;
exit(1);
}
}
// Accessor
int My_array::get_size()
{
return size;
}
void My_array::output()
{
cout << "The array of size " << size
<< " contains the elements:" << endl;
for (int i = 0; i<size; i++)
cout << array[i] << ' ';
cout << endl;
}
//overloading the << operator.
ostream &operator<<(ostream &out, My_array &data)
{
out << "The array of size " << data.size
<< " contains the elements:" << endl;
for (int i = 0; i<data.size; i++)
out << data.array[i] << ' ';
out << endl;
return out;
}
#ifndef MY_ARRAY_H
#define MY_ARRAY_H
#include <iostream>
using namespace std;
class My_array {
protected:
int size;
int *array;
public:
// Constructor
My_array(int the_size = 0);
// Destructor
~My_array();
// Copy constructor
My_array(My_array &data);
// Assignment operator
My_array &operator=(My_array &data);
void input();
void randomize(int limit);
bool operator ==(My_array &data);
bool operator !=(My_array &data);
int hamming(My_array &data);
// Deletes the array
void empty();
// Resize the array.
void resize(int the_size = 0);
// Access an element of the array.
int &operator[](int index);
// Returns the size of the array.
int get_size();
// Output the elements of the array.
void output();
friend ostream &operator<<(ostream &out, My_array &data);
};
#endif
This:
*this[i].array != data[i].array
should be this:
array[i] != data.array[i]
or this:
array[i] != data[i]
The *this is unnecessary, and data[i] is a reference to an int (the same one you get by calling data.array[i], thanks to your operator[]), and an int has no member called "array".

My swap program is not working using Template?

I'm using Template keyword to run simple swap program, Kindly help me why my program is not working out?
#include<iostream>
using namespace std;
template<typename T>
void Swap(T m, T n)
{
T temp;
temp = m;
m = n;
n = temp;
}
int main()
{
int i = 5, j = 6;
cout << "Before swapping:" << endl;
cout << i << " and " << j << endl;
Swap(i, j);
cout << "After Swapping:" << endl;
cout << i << " and " << j << endl;
return 0;
}
Output:
You are creating copies of your arguments because you are taking them by value. If you want to update them within the function you should take them by reference:
void Swap(T& m, T& n)
// ^ ^
Additionally, your implementation has an implicit constraint that T must be default-constructible, which is not necessary for swapping. You should construct your temp variable directly:
T temp = m;
You'd also be better off using move semantics to avoid making copies:
T temp(std::move(m)); // or T temp = std::move(m);
m = std::move(n);
n = std::move(temp);