Segmentation fault on linux (g++) but not on Mac OS.? - c++

First of all I would like to apologize for my large code. I tried to keep it structured, but I am still new to programming in C++.
I created a C++ algorithm on OSX and it worked just fine. I need to run this program on Linux however. Compiling on Linux gave no errors, however when I run the program it gives the following error:
Segmentation fault (core dumped)
I am a newbie in debugging code and have tried to debug it with gdb, but I don't know how I should continue. The information gdb gives is the following:
c2f_function(new_candidates2, old_candidates, feature_list);
(gdb)
Program received signal SIGSEGV, Segmentation fault.
0x0000000000403dc5 in c2f_function (new_candidates=std::list = {...}, old_candidates=std::list = {...},
feature_list=empty std::list) at /home/martin/emc/group4/src/c2f_function.cpp:36
36 norm = iter_old->x -iter_new->x;
I have added the code below, it consists of a main file c2f.cpp, a header file c2f.hpp and an additional file where I store functions c2f_functions.cpp.
The error seems to happen when I pass 3 lists by reference to a function called c2f_functions. this function is within the c2f_functions.cpp script.
My questions are,
how can I solve this?
why does it work well under OSX but not under Linux?
Many thnaks!
MAIN FILE c2f.cpp:
#include "c2f.hpp"
#include "c2f_function.cpp"
int main()
{
// define variables
double x, y;
// create old candidates list
list<Candidate> old_candidates;
// create new candidates list
list<Candidate> new_candidates1;
list<Candidate> new_candidates2;
list<Candidate> new_candidates3;
// create new features list
list<Candidate> feature_list;
//=============================================================================//
// LOAD FIRST DATA SET
//-----------------------------------------------------------------------------//
ifstream file1_("newcandidates_it0.txt");
if (file1_.is_open())
{
cout << "Reading file...1 " << endl;
while( file1_ >> x >> y)
{
// cout << x << "," << y << endl;
new_candidates1.push_back(Candidate(x , y));
}
file1_.close();
}
else {cout << "file is not open";}
//=============================================================================//
c2f_function(new_candidates1, old_candidates, feature_list);
//=============================================================================//
// LOAD SECOND DATA SET
//-----------------------------------------------------------------------------//
ifstream file2_("newcandidates_it1.txt");
if (file2_.is_open())
{
cout << "Reading file...2 " << endl;
while( file2_ >> x >> y)
{
// cout << x << "," << y << endl;
new_candidates2.push_back(Candidate(x , y));
}
file2_.close();
}
else {cout << "file is not open";}
//=============================================================================//
c2f_function(new_candidates2, old_candidates, feature_list);
HEADER FILE c2f.hpp
# include <iostream>
# include <stdlib.h>
# include <string>
# include <math.h>
# include <Eigen/Dense>
# include <cstdio>
# include <cstdlib>
# include <list>
# include <fstream>
# include <algorithm>
// # include <cstdarg>
using namespace std;
using namespace Eigen;
// correspondence margin: new point must lie w/i 10cm from old point
# define CORR_MARGIN 0.1
# define PERSIST_UB 3
# define PERSIST_LB -PERSIST_UB
class Candidate
{
public:
int id;
double x;
double y;
int persistency = 0;
int pflag = 0; // persistency flag
Candidate ( double xNew, double yNew ): x(xNew), y(yNew){}
void increasePersistency()
{
if (persistency < PERSIST_UB) // bound persistency from above
persistency++;
}
void decreasePersistency()
{
if (persistency > PERSIST_LB) // bound persistency from below
persistency--;
}
// bool operator< (const Candidate& right) const { return id < right.id; }
};
bool ascendingId ( Candidate a, Candidate b)
{
return a.id < b.id;
}
bool descendingId ( Candidate a, Candidate b)
{
return a.id > b.id;
}
bool ascendingPersistency ( Candidate a, Candidate b)
{
return a.persistency < b.persistency;
}
bool descendingPersistency ( Candidate a, Candidate b)
{
return a.persistency > b.persistency;
}
bool ascendingPflag ( Candidate a, Candidate b)
{
return a.pflag < b.pflag;
}
bool descendingPflag ( Candidate a, Candidate b)
{
return a.pflag > b.pflag;
}
bool sameId_Feature (Feature first, Feature second)
{ return first.id == second.id; }
bool samePflag (Candidate first, Candidate second)
{ return first.persistency == second.persistency; }
bool finder (Candidate first, Candidate second)
{return first.id == second.id;}
bool not_persistent (Candidate &a)
{ return (a.persistency==PERSIST_LB); }
Functions File c2f_function.cpp
void print_list(list<Candidate> &list2print)
{
for (auto const &iter : list2print)
{
cout << iter.x
<< "," << iter.y
<< " with id "
<< iter.id
<< " and persistency "
<< iter.persistency
<< endl;
}
}
void c2f_function(list<Candidate> &new_candidates, list<Candidate> &old_candidates, list<Candidate> &feature_list)
{
double norm;
//=============================================================================//
// CHECK FOR CORRESPONDENCE
//-----------------------------------------------------------------------------//
// Check if old candidates exist (initialization purposes)
if (old_candidates.empty() == 0) // old candidates exist
{
// Check Correspondence
for (auto iter_old = old_candidates.begin(); iter_old != old_candidates.end(); iter_old++)
{
// int persistency_upd_flag = 0;
for (auto iter_new = new_candidates.begin(); iter_new != new_candidates.end(); iter_new++)
{
// compute the norm between old_candidates and new_candidates
// norm = sqrt( pow(iter_old->x - iter_new->x, 2.0) + pow(iter_old->y - iter_new->y, 2.0));
norm = iter_old->x -iter_new->x;
if (norm <= CORR_MARGIN)
{
// Update position of old entry and increase persistency
iter_old -> x = iter_new->x;
iter_old -> y = iter_new->y;
iter_old -> increasePersistency();
// flag an update;
iter_old -> pflag = 1;
// remove list entry that has been coupled
new_candidates.erase(iter_new);
}
}
}
}
else
{
back_insert_iterator<list<Candidate>> it(old_candidates);
for (auto const &iter : new_candidates)
{
it = iter;
}
int counter=1;
for (auto iter = old_candidates.begin(); iter!= old_candidates.end(); iter++)
{
iter -> id = counter;
++counter;
}
cout << "initializing data set" << endl;
cout << endl << "====================================================" << endl;
return;
}
//=============================================================================//
//=============================================================================//
// DECREASE PERSISTENCY FOR NON-VIEWED CANDIDATES
//-----------------------------------------------------------------------------//
// remove persistency to non-associated candidates
old_candidates.sort(ascendingPflag);
for (auto iter = old_candidates.begin(); iter!= old_candidates.end(); iter++)
{
if ( iter -> pflag == 0 )
{
iter -> decreasePersistency();
find_if (feature_list.begin(), feature_list.end(),
[iter] (Candidate &item)
{
if (item.id == iter->id)
{
item.persistency = iter->persistency;
return true;
}
else return false;
}
);
}
// reset pflags
iter -> pflag = 0;
}
//=============================================================================//
//=============================================================================//
// ADD id TO REMAINING new_candidates LIST
//-----------------------------------------------------------------------------//
// get new id
old_candidates.sort(descendingId);
int new_id = old_candidates.begin() -> id + 1;
// add id to added items to old_candidates
for (auto iter = new_candidates.begin(); iter!= new_candidates.end(); iter++)
{
iter -> id = new_id;
new_id++;
}
//=============================================================================//
//=============================================================================//
// MERGE REMAINING new_candidates WITH old_candidates LIST
//-----------------------------------------------------------------------------//
old_candidates.splice(old_candidates.end(), new_candidates);
//=============================================================================//
//=============================================================================//
// ADD TO feature_list
// REMOVE FROM feature_list
// REMOVE FROM old_list
//-----------------------------------------------------------------------------//
// removing from old_candidates when persistency # lower bound
old_candidates.sort(ascendingPersistency);
for (auto const &iter_old : old_candidates)
{
if (iter_old.persistency == PERSIST_LB)
{
old_candidates.pop_front();
}
else
{break;}
}
// removing from feature_list when persistency # lower bound
feature_list.sort(ascendingPersistency);
for (auto const &iter_feat : feature_list)
{
if (iter_feat.persistency == PERSIST_LB)
{
feature_list.pop_front();
}
else
{break;}
}
// sorting
old_candidates.sort(descendingPersistency);
// adding
back_insert_iterator<list<Candidate>> it(feature_list);
// define counter
int counter;
for (auto const &iter_old : old_candidates)
{
counter =0;
if (iter_old.persistency == PERSIST_UB)
{
if (feature_list.size()>0)
{
for (auto iter_feat = feature_list.begin(); iter_feat != feature_list.end(); iter_feat++)
{
if (iter_feat->id == iter_old.id)
{
iter_feat->x = iter_old.x;
iter_feat->y = iter_old.y;
iter_feat->persistency = iter_old.persistency;
counter = 0;
break;
}
else
{
counter++;
}
}
if (counter >0)
{
it = iter_old;
}
}
else
it = iter_old;
}
else
{
break;
}
}
//=============================================================================//
//=============================================================================//
// DISPLAY FEATURE LIST
//-----------------------------------------------------------------------------//
if (feature_list.size() > 0)
{
feature_list.sort(ascendingId);
cout << "Feature members" << endl;
print_list(feature_list);
cout << endl << "====================================================" << endl;
}
else
cout << endl << "====================================================" << endl;
//=============================================================================//
}
//*****************************************************************************//
//*****************************************************************************//

SYSSEGV Segmentation Fault is caused by an attempt to access memory outside of the program's allowed area. In this case, either iter_old or iter_new is not initialized or contains a value that does not correspond to the program's memory area.
It may crash on one computer system and not on another because 1) different systems can have different values in uninitialized variables and 2) different systems define the programs available memory differently.
In short, look for bad pointer values with SEGV errors, and know that bugs can appear in different ways on different systems.

I'm not sure but I suspect the problem is that you erase an iterator and, next, you use (increment) it.
The following is the crucial part
for (auto iter_new = new_candidates.begin(); iter_new != new_candidates.end(); iter_new++)
{
norm = iter_old->x -iter_new->x;
if (norm <= CORR_MARGIN)
{
// [...]
new_candidates.erase(iter_new);
}
}
When you erase(iter_new), iter_new become an iterator pointing to an invalid object; incrementing it (iter_new++) give you (if I'm not wrong) an undefined value and the following iter_new->x can segmentation fault your program.
I suppose that a solution can be the use of the postfix increment calling erase() so that erase() call a copy of iter_new and iter_new is incremented to a valid iterator before the call to erase(); something like
auto = new_candidates.begin();
while ( iter_new != new_candidates.end() )
{
norm = iter_old->x -iter_new->x;
if (norm <= CORR_MARGIN)
{
// [...]
new_candidates.erase( iter_new++ );
}
else
++iter_new;
}
p.s.: sorry for my bad English

Related

I want to combine the list and find(), but I don't know how to merge them

Please see the part where the find() function is called. (I ran it in Visual Studio.)
Code:
using namespace std;
int main(void) {
//Implementing the find()
/*
bool Find(const Stu & a); {
return (*it).name;
}
*/
list<Astu>::iterator that;
//that = astu.begin();
//that = find(astu.begin(), astu.end(), (*it).name);
for (it = stu.begin(); it != stu.end(); it++) {
that = find_if(astu.begin(), astu.end(), (*it).name);
if (that != astu.end()) {
all = astu.erase(all);
all++;
}
else
all++;
}
/*
//Filter absenteeism from the total student roster
for (it = stu.begin(); it != stu.end(); it++) {
for (all = astu.begin(); all != astu.end();) {
if (!strcmp((*all).name, (*it).name)) {
//Delete attendees and latecomers from the list
all = astu.erase(all);
}
else
all++;
}
}
*/
cout << "---------------------\n결석자: " << endl;
//이름순으로 정렬
astu.sort(SizeComp2);
//결석자들 출력
for (all = astu.begin(); all != astu.end(); all++) {
cout << "이름: " << (*all).name << endl;
}
return 0;
}
Output:
C2064 error occurred: Term does not evaluate to a function that takes
1 argument.
Even with find_if() in this code, there is a problem. The bool() part in the comment was used for the find_if object, but it doesn't seem to be used well here.
I deleted the part where there was no problem as a result of debugging. You must use an unconditional list, not a vector.
Where should I fix it?
The third argument to std::find_if is a function.
You could use a lambda as the function:
auto that = find_if(astu.begin(), astu.end(), [it](Astu const& astu)
{
return astu.name == it->name;
});
[This assumes that Astu::name is a std::string]

Delete and insert an element from/to array bag. Why boolean array instead of int?

I am implementing bag using array in C++. I can not figure out how to let the deleteElement function work. It is suppose to delete given element from the array.
I also don't understand why the array is initialized with bool and how the insert function works.
So, I got three questions:\
How to make the deleteElement function work?
Why is the array initialized with bool?
How does the insert function work? It looks like it only adds true value to the array, but when this program prints the array, you will see that the x value is printed out, I can not figure this out.
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
// cin -> add 0 qry 0 del 0 qry 0 quit
// cout -> TF
// add x -> Adds the number x to the bag
// qry x -> Checks if x belongs to the bag then output T, otherwise output F
// del x -> If there is an element x in the bag, remove it, otherwise do nothing.
// quit -> Stops the program
// Exercise: Fun with bags 1 (Here the bag is a set of int values).
/*
Example:
input: add 1 add 2 add 1 del 1 qry 1 qry 2 quit
output: FT
*/
// enumeration type for actions on the bag
enum action {add, qry, del, quit, none};
// translation of strings into actions
action str2action(string s) {
if (s=="add") return add;
if (s=="qry") return qry;
if (s=="del") return del;
if (s=="quit") return quit;
return none;
}
#define BAG_AS_ARRAY_SIZE 10
struct bag {
bool as_array[BAG_AS_ARRAY_SIZE]; // using arrays
};
// Simple function to initialise the bag
void initialise(bag &b){
// Array
for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
b.as_array[i] = false;
}
}
// function to display the content of the bag
void display_bag(bag b) {
cout << "The bag is : " << endl;
// Array
cout << " - (A) - : " ;
for(int i=0; i<BAG_AS_ARRAY_SIZE; i++){
if(b.as_array[i])
cout << i << " " ;
}
cout << endl;
return;
}
void insert(bag &b,unsigned int x){ //add
// Array
b.as_array[x] = true;
}
void check(bag &b,unsigned int x) //qry
{
bool q = false;
for(int i = 0; i < BAG_AS_ARRAY_SIZE; i++)
{
if(b.as_array[x])
{
q = true;
}
}
if(q == true)
{
cout << "T";
}
if(q == false)
{
cout << "F";
}
cout << endl;
}
void DeleteElement(bag &b, unsigned int x) //del
{
int i;
for (i=0; i<BAG_AS_ARRAY_SIZE; i++)
if (b.as_array[i] == x)
break;
if (i < BAG_AS_ARRAY_SIZE)
{
for (int j=i; j<BAG_AS_ARRAY_SIZE; j++)
b.as_array[j] = b.as_array[j+1];
}
}
// this function deals with actions on a bag
void update(bag &b, action a, unsigned int x){
switch(a){
case add:
insert(b,x);
break;
case qry:
check(b,x);
break;
case del:
DeleteElement(b,x);
break;
case quit:
break;
case none:
break;
default:
break;
}
return;
}
int main()
{
bag my_bag; //We create an array of boolean type.
string my_act_str;
unsigned int x;
initialise(my_bag); //The array is initialised with False, which is 0
bool go_on = true;
while(go_on)
{
display_bag(my_bag);
cout << "What's next? (actions = add x ,qry x ,del x ,quit)" << endl;
cin >> my_act_str;
action act = str2action(my_act_str);
if(act == quit)
{
go_on = false;
}
if(act == add)
{
cin >> x;
update(my_bag,act,x);
}
if(act == qry)
{
cin >> x;
update(my_bag,act,x);
}
if(act == del)
{
cin >> x;
update(my_bag,act,x);
}
}
return 0;
}
Edit:
I found out solution for the delete function. It is very easy one:
void delete_element(bag &b, unsigned int x)
{
b.as_array[x] = false;
}
Your three questions actually come from the fact that this is not really a bag. What you have here is more like a "Boolean mask" that indicates if numbers from zero to BAG_AS_ARRAY_SIZE - 1 are true or false. That is why you have a Boolean array as the storage and all elements in it are initialized with false. That is, the mask is not set for any of the numbers from zero to BAG_AS_ARRAY_SIZE - 1.
Your deleteElement function then only needs to set the corresponding array position of the mask to false to "delete" that number and "inserting" a number corresponds to setting that specific position in the mask to true.
In the display_bag function, notice that you are not print the content of the array (which obviously can only be either true or false), but the index of the positions in the array that have a true value.

Incorrect count output / Having difficulty trying to create a HashTable/Set using open addressing

I'm trying to create a program that opens a .txt file containing a speech and assigns each word to a space in the array/set based on the hash value. Collisions are accounted for using open addressing method. The program should be able to perform the following functions: add(), remove(), find(), count() which keeps count of the elements IN the array/set, and loadfactor(). A template header.h file was provided that required some filling in, but my unfamiliarity with that style of coding was making it difficult for me to understand it. Below I have provided the code I have so far, and everything seems to be working except the mCount. The speech contains about 300 words but when I run the code, the count output shows 17. I'm assuming the error is in my resizing function but I am unsure.
//hashset.h file
#pragma once
#include <cmath>
#include <functional>
#include <vector>
template <typename TValue, typename TFunc>
class HashSet
{
private:
// Unlike Java, the C++ hashtable array won't be full of null references.
// It will be full of "Entry" objects, each of which tracks its current state
// as EMPTY, FULL (with a value), or NIL (value was once here, but was removed).
class Entry
{
public:
enum EntryState { EMPTY, FULL, NIL };
TValue value;
EntryState state;
Entry() : value(), state(EMPTY) {}
Entry(const TValue& v) : value(v), state(EMPTY) {}
};
TFunc mHash;
std::vector<Entry> mTable;
std::size_t mCount;
public:
// Constructs a hashtable with the given size, using the given function for
// computing h(k).
// hash must be a callable object (function, functor, etc.) that takes a parameter
// of type TValue and returns std::size_t (an integer).
HashSet(int size, TFunc hash) : mHash(hash)
{
// initialize count
mCount = 0;
// hashtable array cannot be same data type as that of what is being stored (cannot be string)
// requirement #4 - if user inputs array size that is not a power of 2, constructor must round to nearest power of 2 value
size = pow(2, (int(log(size - 1) / log(2)) | 1));
mTable.resize(size); // resizes the vector to have given size.
// Each element will be default-constructed to have state EMPTY.
}
void resize(int new_size) {
HashSet aux{ new_size, mHash }; //double the size, same hash function
for (const auto& entry : mTable)
if (entry.state == Entry::FULL && entry.state == Entry::EMPTY && entry.state == Entry::NIL) //there is an element
aux.add(entry.value); //insert it on the new set
*this = aux;
}
// Inserts the given value into the set.
void add(const TValue& value)
{
// Use the type std::size_t for working with hash table indices.
// Invoke the mHash function, passing the key to calculate h(k), as in
// size_t hashCode = mHash(value);
// Mod down to size.
// Go to the table at that index and do the insertion routine.
// Note, if table is full when trying to add an element, it should double in size
// to keep table size a power of 2
if (double(mCount) / mTable.size() > 0.8) // load factor comparison
this->resize(2 * mTable.size()); // call resize function if array is too small to accommodate addition
size_t hashCode = mHash(value) % mTable.size(); // mod value by table size to get starting index
if (mTable[hashCode].state == Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { // NIL space CAN be replaced with value
mTable[hashCode].value = value; // store value in vector index specified by hashCode
mCount++; // increment counter when word is added
}
else {
for (std::size_t i = 1; i < mTable.size(); i++) {
// use open addressing to find next open space
if (mTable[hashCode].state != Entry::EMPTY) {
hashCode = ((mHash(value) % mTable.size()) + ((int)(pow(i, 2) + i) >> 1)) % mTable.size(); // h(k) + f(i) or h(k) + ((i^2 + i)) / 2
}
else if (mTable[hashCode].value == value) { // account for duplicates
break; // exit for-loop
}
else if (mTable[hashCode].state == Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { // NIL space CAN be replaced with value
mTable[hashCode].value = value; // store value in vector index specified by new hashCode
mCount++; // increment counter when word is added
break; // exit for-loop
}
else
break; // exit for-loop
}
}
}
// Returns true if the given value is present in the set.
bool find(const TValue& key)
{
size_t hashCode = mHash(key) % mTable.size(); // mod value by table size to get starting index to do retrace
if (mTable[hashCode].value == key)
return true;
else if (mTable[hashCode].state != Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { // check that set is not empty or has a NIL state
for (std::size_t i = 1; i < mTable.size(); i++) {
// use open addressing again to find key
if (mTable[hashCode].value != key)
hashCode = ((mHash(key) % mTable.size()) + ((int)(pow(i, 2) + i) >> 1)) % mTable.size();
else if (mTable[hashCode].value == key) {
return true; // value found at speecified location
break; // exit for-loop as first instance of value has been found
}
//else if (i == mTable.size()) // end of table reached, element not in set
//return false;
}
}
else // end of table reached, element was not in set
return false;
}
// Removes the given value from the set.
void remove(const TValue& key)
{
size_t hashCode = mHash(key) % mTable.size(); // mod value by table size to get starting index to do retrace
if (mTable[hashCode].value == key) {
mTable[hashCode].value = Entry::NIL; // replace value with NIL so find() op does not return a false when searching for element
mCount--; // decrement element counter
}
else if (mTable[hashCode].state != Entry::EMPTY || mTable[hashCode].state != Entry::NIL) { // check that there is a value to be removed
for (std::size_t i = 1; i < mTable.size(); i++) {
// use open addressing again to find key
if (mTable[hashCode].value != key) {
hashCode = ((mHash(key) % mTable.size()) + ((int)(pow(i, 2) + i) >> 1)) % mTable.size();
}
else {
mTable[hashCode].value = Entry::NIL; // if found after open addressing, replace with NIL
mCount--; // decrement element counter
}
}
}
}
int count() {
return mCount;
}
double loadFactor() {
double a = double(mCount) / mTable.size();
return a;
}
};
// main function
#include "hashset.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string testline;
vector<string> word;
HashSet<std::string, std::hash<std::string> > obj1{ 50, std::hash<std::string>{} };
ifstream Test("speech.txt");
if (!Test)
{
cout << "There was an error opening the file.\n";
return 0;
}
//store words in vector
while (Test >> testline) {
word.push_back(testline);
//obj1.add(testline);
}
//output whole vector with position numbers for each entry
cout << "Array contents:\n";
for (int i = 0; i < word.size(); i++) {
obj1.add(word[i]);
cout << word[i] << "(" << i << ")" << endl;
}
cout << "current count: " << obj1.count() << endl;
obj1.add("abcd"); // should hash to 4
if (obj1.find("abcd"))
cout << "abcd is in the set " << endl;
else
cout << "abcd is not in set " << endl;
obj1.add("adcb"); // should hash to 4 then 5 after probing
if (obj1.find("adcb"))
cout << "adcb is in the set " << endl;
else
cout << "adcb is not in set " << endl;
obj1.add("acde"); // should hash to 4 then 7 after probing
if (obj1.find("acde"))
cout << "acde is in the set " << endl;
else
cout << "acde is not in set " << endl;
obj1.remove("adcb"); // 5 should have NIL
if (obj1.find("adcb"))
cout << "adcb is in the set " << endl;
else
cout << "adcb is not in set " << endl;
if (obj1.find("acde"))
cout << "acde is still in the set " << endl;
else
cout << "acde is not in set " << endl;
cout << "final count: " << obj1.count() << endl;
system("pause");
exit(0);
}
}
The errors around NIL are because the enum defining NIL is part of the Entry class. You need to prefix NIL with the class name so the compile knows where the keyword comes from.
else if (mTable[hashCode] != NULL || mTable == Entry::NIL) { // getting error NIL identifier not found
The HashSet variable declaration is complaining because you are passing the wrong types. HashSet constructor takes a size and and a hash function. You are passing it a size and a string. Note the comment above the HashSet constructor
// hash must be a callable object (function, functor, etc.) that takes a parameter
// of type TValue and returns std::size_t (an integer).
This is your clue how to construct a HashSet object.

Return struct element from vector c++

I'm new to C++ and I'm trying to return a struct from a vector of structs by using 2 search criteria.
The function find_city is returning me everything from the defined range, regardless of whether it exists inside the vector of struct.
Here's my code:
struct cityLoc
{
int hRange;
int vRange;
int cityCode;
string cityName;
};
vector<cityLoc> cl1;
// the vector has already been preloaded with data
// function to return my struct from the vector
cityLoc find_city(int hRange, int vRange)
{
for (size_t i = 0; i < cl1.size(); i++)
{
if ((cl1[i].hRange = hRange) && (cl1[i].vRange = vRange))
{
return cl1[i];
}
}
}
int main()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j <= 8; j++)
{
cityLoc this_city;
this_city = find_city(i, j);
cout << this_city.hRange << ", " << this_city.vRange << endl;
}
}
return 0;
}
Also, aside from this question, I was previously looking into std::find_if and didn't understand it. If I have the following code, what is the output? How do I modify it such that it returns a struct?
auto it = find_if(cl1.begin(), cl1.end(), [](cityLoc& cl) { return cl.hRange == 1; } );
You have a bug here:
if ((cl1[i].hRange = hRange) && (cl1[i].vRange = vRange))
Those = are assignments, not comparisons! Please enable compiler warnings and you won't be hurt by such obvious typos in future.
std::find_if will return the iterator to the found struct entry if it is successful, std::vector::end() otherwise. So, you should first validate the returning iterator if it is valid or not.
For example:
auto it = std::find_if( cl1.begin(), cl1.end(),
[](const cityLoc& cl) { return cl.hRange == 1; } );
if ( it == cl1.end() )
{
// ERROR: Not found! Return error code etc.
return -1;
}
// And, if found, process it here...
std::cout << it->hRange << '\n';
std::cout << it->vRange << '\n';
The criteria (predicate) part in std::find_if is a lambda expression.

Synchronizing MapB to MapA in C++

I have a std::map that I generate from a json and a map that I generate from sqlite.
I want to compare the two maps and make changes to the sqlite so that it matches the json. I originally used the map.find(key) method through both maps to figure out what to add and what to delete but my friend told me that map was sorted from least to greatest key and so I could just run through it once.
I came up with two methods. Do you have any advice on which algorithm would be preferred and why? I am thinking the one I have uncommented is faster as (and please correct me if I'm wrong) I believe the uncommented one is O(n) worst case while the latter is O(n^2) worst case.
Also, my friend had mentioned that I didn't need the second 'clean up' while loop to reconcile the remaining sqlMap items, but I really think I need it. Is he right?
Here's my code:
void SqlSync::syncEvents() {
int added = 0;
int replaced = 0;
int deleted = 0;
int skipped = 0;
// get categories from Apsiva
std::map<int, Event> jsonMap = _apsivaRest->getEvents();
// get categories from sqlite
std::map<int, Event> sqlMap = _sqliteConnection->getEventMap(true);
// COMPARE
map<int, Event>::iterator jsonIter = jsonMap.begin();
map<int, Event>::iterator sqlIter = sqlMap.begin();
while (jsonIter != jsonMap.end() && sqlIter != sqlMap.end()) {
int jsonId = jsonIter->first;
Event jsonObj = jsonIter->second;
int sqlId = sqlIter->first;
if (jsonId < sqlId) {
// add
_sqliteConnection->addEvent(jsonObj);
++added;
++jsonIter;
} else if (jsonId > sqlId) {
// remove
_sqliteConnection->deleteEvent(sqlId);
++deleted;
++sqlIter;
} else {
if (jsonObj.isNewerThan(sqlIter->second)) {
_sqliteConnection->updateEvent(jsonObj);
++replaced;
} else {
// ignore
cout << "Skipped event b/c not newer" << endl; // delete when verified
++skipped;
}
++jsonIter;
++sqlIter;
}
}
// int jRemaining = std::distance(jsonIter, jsonMap.end());
// int sRemaining = std::distance(sqlIter, sqlMap.end());
// add remaining jsonMap Objects
while (jsonIter != jsonMap.end()) {
Event jsonObj = jsonIter->second;
_sqliteConnection->addEvent(jsonIter->second);
++added;
++jsonIter;
}
// delete remaining sqlMap Objects
while (sqlIter != sqlMap.end()) {
_sqliteConnection->deleteEvent(sqlIter->first);
++deleted;
++sqlIter;
}
// OLD WAY TO COMPARE.
// // add/replace keys found in json
// for (map<int, Event>::const_iterator jsonIter = jsonMap.begin(); jsonIter != jsonMap.end(); ++jsonIter) {
// map<int,Event>::const_iterator it = sqlMap.find(jsonIter->first);
// Event jsonObj = jsonIter->second;
// if (it != sqlMap.end()) {
// Event sqlObj = it->second;
// if (jsonObj.isNewerThan(sqlObj)) {
//// _sqliteConnection->updateEvent(jsonObj);
// ++replaced;
// } else {
// // ignore
// cout << "Skipped category b/c not newer" << endl; // delete when verified
// ++skipped;
// }
// } else {
//// _sqliteConnection->addEvent(jsonObj);
// ++added;
// }
// }
//
// // delete sqlmap CategoryRows not in jsonMap
// for (map<int, Event>::const_iterator sqlObj = sqlMap.begin(); sqlObj != sqlMap.end(); ++sqlObj) {
// if (jsonMap.find(sqlObj->first) == jsonMap.end()) {
//// _sqliteConnection->deleteEvent(sqlObj->first);
// ++deleted;
// }
// }
#ifdef DEBUG
cout << "CATEGORIES SYNC:" << endl;
cout << "---------------" << endl;
cout << "Added: " << added << " | Replaced: " << replaced
<< " | Deleted: " << deleted << " | Skipped: " << skipped << endl;
#endif //DEBUG
}
The uncommented way is more efficient. The complexity will be O(n+m) when n and m are sizes of json and SQLite maps.
You will need the last loop, since when you exit the first loop you don't know which map end you reached first. Consider next case - json map has ids 1,2,4,5 and SQLite has ids 1,2,6,7.
You will need the last loop in order to delete items 6 and 7.