Deleting and object in a position from a deque - c++

I have this code:
bool tuple_compare(boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> &tuple_from_done)
{
for(int i = 0; i < deque_wait.size(); i++) {
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple_from_wait = deque_wait.at(i);
ppa::Node *father = boost::get<0>(tuple_from_wait);
ppa::Node *son = boost::get<0>(tuple_from_wait);
ppa::Node *second_son = boost::get<2>(tuple_from_wait);
bool has_seq = boost::get<3>(tuple_from_wait);
cout << "checking this two " << boost::get<1>(tuple_from_wait)->get_name() << " bool sequence "
<< boost::get<1>(tuple_from_wait)->node_has_sequence_object << " and this "
<< boost::get<2>(tuple_from_wait)->get_name() << " bool seq " << boost::get<2>(tuple_from_wait)->node_has_sequence_object
<< " with " << boost::get<0>(tuple_from_done)->get_name() << endl;
if(boost::get<0>(tuple_from_done)->get_name() == boost::get<1>(tuple_from_wait)->get_name()
|| boost::get<0>(tuple_from_done)->get_name() == boost::get<2>(tuple_from_wait)->get_name())
{
cout << " found in here this we need to check if there is something if the sons have a sequences!!!! " << endl;
if(boost::get<1>(tuple_from_wait)->node_has_sequence_object == true && boost::get<2>(tuple_from_wait)->node_has_sequence_object == true)
{
cout << " ding, ding, we have one ready!!!" << endl;
return true;
}
else
{
cout << "not ready yet" << endl;
}
}
}
return false;
}
Now I need to delete the object that is found in the line "ding, ding", but I don't know how to do it, I know the iterators are used well I actually have to delete this tuple from the deque_wait and move it to the deque_run, but I don't really understand those yet, so can you help me, thanks.

deque_wait.erase(deque_wait.begin() + i);
// ^^^^^^^^^^^^^^^^^^^^^^
// that's an iterator
deque supports random access iterators, which are very much like pointers (in fact, pointers are a type of random access iterator), so you can just get the begin iterator and add an integer to it to get the offset, just like you could do with a pointer.

Related

Confused about behavior when reading stdin c++

I am attempting to solve a problem to improve my C++ skills. I have come across some unusual behavior and I am not sure why it is happening.
I initially parsed the stdin and sorted it into a map with a key and a matching element (value). I then want to read a list of keys and output the element (value). However, for some of the key names it returns the correct value but for others it does not, but, if I hard code the intended key it works.
I know I am parsing correctly since the correct key and value are in the map as shown at the end of my output.
I assume it is an issue with reading stdin since the boolean str.compare("tag1.tag2~name") == 0 never evaluates to 0.
The code is as follows:
//'it' is the name of my map
//q = 3
for (int i = 0; i < q; i++)
{
getline(cin, str);
if (str.compare("tag1.tag2~name") == 0)
{
cout << "key is found" << endl;
}
else
{
cout << "'" << str << "'" << endl;
}
if (it.count(str) == 0)
{
cout << "Not Found!" << endl;
}
else
{
cout << it[str] << endl;
}
}
map<string, string>::iterator ptr;
cout << "\nThe map it is : \n";
cout << "\tKEY\tELEMENT\n";
for (ptr = it.begin(); ptr != it.end(); ++ptr) {
cout << '\t' << ptr->first
<<"\t" + ptr->second << "\n";
}
return 0;
The output is:
'tag1.tag2~name
Not Found!
'tag1~name
Not Found!
'tag1~value'
HelloWorld
The map it is :
KEY ELEMENT
tag1.tag2~name Name1
tag1~value HelloWorld
The input is :
tag1.tag2~name
tag1~name
tag1~value
Thank you! I have been struggling with this for a while.

c++: How can I print vector elements' indexes every time I print the vector?

So, I have a vector of boats. I need to access these boats and modify them (i.e. delete them) regularly, so it would be really nice if I could print their index along with all their other information, but I can't seem to figure out how.
The closest I got to it was with a simple for loop, but that eventually prints the current index along with the previous ones, as the vector size grows (since my i was < vector.size())
vector <Boat> berths_reg;
//print vector elements info
void Boat::print_info()
{
cout << endl;
for(int i = 0; i < berths_reg.size(); i++)
{
cout << "Index : " << i << endl;
}
cout << "Boat type : " << type << endl;
cout << "Boat length : " << length << endl;
cout << "Draft depth : " << draft << endl;
cout << endl;
}
//iterate through vector to print all elements
void print_vector()
{
vector <Boat> ::iterator it;
for (it = berths_reg.begin(); it != berths_reg.end(); ++it)
{
it->print_info();
}
}
//Storing boats (objects data) inside vector
void add_boat(Boat* b, string type, int length, int draft)
{
b->get_type(type);
b->get_length(length);
b->get_draft(draft);
berths_reg.push_back(*b);
}
Simply print both the index and the info within the same loop:
void print_vector()
{
for(int i = 0; i < berths_reg.size(); ++i)
{
cout << "Index : " << i << endl;
berths_reg[i].print_info();
}
}

Not sure where the segmentation fault is

I'm getting problem with segmentation fault when trying to compile a C++ program, but not sure where the problem lies. I suspect that the problem lies with the .find() ..... could it be the iterator operator < and == which are the comparators for find() that is the issue? I hope that someone can point out to me where they think the problem lies.
The following is part of test01.cpp, where I run it to test the code and use print statements to find out where the problem is:
bool confirmEverythingMatches(const btree<long>& testContainer, const set<long>& stableContainer) {
cout << "Confirms the btree and the set "
"contain exactly the same values..." << endl;
for (long i = kMinInteger; i <= kMaxInteger; i++) {
cout << "Start of for-loop to find iterator for comparisons..." << endl;
if (stableContainer.find(i) != stableContainer.end()) {
cout << "can find i (" << i << ") in stableContainer!" << endl;
} else {
cout << "cannot find i (" << i << ") in stableContainer!" << endl;
}
cout << "In between finding i in stable and testContainers..." << endl;
if (testContainer.find(i) != testContainer.end()) {
cout << "can find i (" << i << ") in testContainer!" << endl;
} else {
cout << "cannot find i (" << i << ") in testContainer!" << endl;
}
cout << "Before assigning the find to boolean variables..." << endl;
bool foundInTree = (testContainer.find(i) != testContainer.end());
cout << "testContainer.find(i) != testContainer.end()" << endl;
bool foundInSet = (stableContainer.find(i) != stableContainer.end());
cout << "stableContainer.find(i) != stableContainer.end()" << endl;
if (foundInTree != foundInSet) {
cout << "- btree and set don't contain the same data!" << endl;
cout << "Mismatch at element: " << i << endl;
return false;
} else {cout << "foundInTree == foundInSet!!!" << i << endl;}
}
cout << "- btree checks out just fine." << endl;
return true;
}
} // namespace close
/**
* Codes for testing various bits and pieces. Most of the code is commented out
* you should uncomment it as appropriate.
**/
int main(void) {
// initialise random number generator with 'random' seed
initRandom();
cout << "after initRandom().." << endl;
// insert lots of random numbers and compare with a known correct container
btree<long> testContainer(99);
cout << "after specifying max node elements in testContainer.." << endl;
set<long> stableContainer;
cout << "after constructing stableContainer.." << endl;
insertRandomNumbers(testContainer, stableContainer, 1000000);
cout << "after inserting random numbers into testContainer and for success inserts, also into stableContainer.." << endl;
btree<long> btcpy = testContainer;
cout << "after copy assigning a copy of testContainer to btcopy.." << endl;
confirmEverythingMatches(btcpy, stableContainer);
cout << "after confirming everything internally matches between testContainer and stableContainer.." << endl;
return 0;
}
The output I get when running the program (No problem when compiling) is this:
Confirms the btree and the set contain exactly the same values...
Start of for-loop to find iterator for comparisons...
cannot find i (1000000) in stableContainer!
In between finding i in stable and testContainers...
ASAN:DEADLYSIGNAL
=================================================================
==7345==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000018 (pc 0x000108d132a8 bp 0x000000000000 sp 0x7fff56eee6f0 T0)
#0 0x108d132a7 in btree<long>::find(long const&) const (test01+0x1000022a7)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (test01+0x1000022a7) in btree<long>::find(long const&) const
==7345==ABORTING
Abort trap: 6
I also got this error when I tried to run it on another machine:
==29936==ERROR: AddressSanitizer failed to allocate 0x200000 (2097152) bytes of SizeClassAllocator32: 12
I found that when it goes into the find(), it will have the segmentation fault:
/**
* Identical in functionality to the non-const version of find,
* save the fact that what's pointed to by the returned iterator
* is deemed as const and immutable.
*
* #param elem the client element we are trying to match.
* #return an iterator to the matching element, or whatever the
* const end() returns if no such match was ever found.
*/
template<typename T> typename btree<T>::const_iterator
btree<T>::find(const T& elem) const {
std::cout << "CONST ITERATOR'S FIND" << std::endl;
Node *tmp_ = root_;
std::cout << "1" << std::endl;
while(true) {
std::cout << "2" << std::endl;
size_t i;
std::cout << "3" << std::endl;
// go through all elements from root to tail
for (i = 0; i < tmp_->__occupied_size_; ++i) {
std::cout << "4" << std::endl;
if (tmp_->__elem_[i] == elem) {
std::cout << "5" << std::endl;
// find the elem, return an iterator
return const_iterator(tmp_, i, this);
std::cout << "6" << std::endl;
} else if (tmp_->__elem_[i] > elem) {
std::cout << "7" << std::endl;
// elem is not in current Node, go to descendants
// for the elem.
if (tmp_->__descendants_ == nullptr) {
std::cout << "8" << std::endl;
return cend();
std::cout << "9" << std::endl;
} else {
std::cout << "10" << std::endl;
tmp_ = tmp_->__descendants_[i];
std::cout << "11" << std::endl;
break;
}
}
}
// handling boundaries cases
if (i == tmp_->__occupied_size_) {
std::cout << "12" << std::endl;
if (tmp_->__descendants_[i] == nullptr) {
std::cout << "13" << std::endl;
return cend();
std::cout << "14" << std::endl;
} else {
std::cout << "15" << std::endl;
tmp_ = tmp_->__descendants_[i];
}
}
}
}
The print statements for this find is:
CONST ITERATOR'S FIND
1
2
3
4
4
7
10
11
2
3
4
7
10
11
ASAN:DEADLYSIGNAL
Ok, so based on the implementation of this find function, I think the problem might be located in
if (tmp_->__descendants_ == nullptr) {
std::cout << "8" << std::endl;
return cend();
std::cout << "9" << std::endl;
} else {
std::cout << "10" << std::endl;
tmp_ = tmp_->__descendants_[i];
std::cout << "11" << std::endl;
break;
}
and then
// handling boundaries cases
if (i == tmp_->__occupied_size_) {
std::cout << "12" << std::endl;
if (tmp_->__descendants_[i] == nullptr) {
std::cout << "13" << std::endl;
return cend();
std::cout << "14" << std::endl;
} else {
std::cout << "15" << std::endl;
tmp_ = tmp_->__descendants_[i];
}
}
So, You are checking if tmp->__descendants_ is not null. If it is not, then you set tmp_ = tmp_->descendants_[i];
Note: you are just checking __descendants_ pointer to be null or not, you are not checking the __descendants_ [i] if it is null!
What if the tmp->__descendants_[i] is null (or gets out of the descendants array)?
If that value is null, then tmp_->occupied_size_ might give you segfault.
Note2: For some reason you are using same index "i" for iterating through __elem_ and __descendants_. I am not sure, how descendants are created, but it might be also a problem here.
This is why debuggers exist. Run your program in the debugger, let the program fail, and then the debugger will tell you where and why it's gone wrong.
It looks like you've got potentially a lot of code to trawl through, which no one here will really want to do as it's not a concise question.
Good luck!

I get these random seg faults on netbeans

I'm doing c++ and the code is giving me these errors:
new size of the done 64
getting next item to do (0x90b9c0 0x90bab0 0x90be40 0)
vector wait size = 0
running size = 1
done size = 64
done this align #27# #26# Gorilla_gorilla_2
setting the done tuple to 1 to get them next
new size of the done 65
getting next item to do (0xbff3897200000000 0xbfda1b75f84c0030 0x30 17)
vector wait size = 18446744073709551615
running size = 1
done size = 65
getting next item to do (0xbfdcc288867fc50b 0x31 0x1 1)
/usr/local/netbeans-7.2rc1/ide/bin/nativeexecution/dorun.sh: line 33: 8234
Segmentation
fault (core dumped) sh "${SHFILE}"
And I wonder how can I get more details of the seg fault, like the line that caused it, to fix it, thanks.
Edit:
bool tuple_compare(boost::tuple< ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple)
{
if( boost::get< 3 >( tuple) == true)
{
return true;
}
else
{
return false;
}
}
void threaded_function(Model_factory &mf, ppa::Node *root)
{
try {
while(true)
{
boost::mutex::scoped_lock lock(result_mutex);
if(wait.empty())
{
lock.unlock();
break;
}
else {
lock.unlock();
lock.lock();
if(!running_jobs.empty())
{
cout << "vector wait size = " << wait.size() << endl;
cout << "running size = " << running_jobs.size() << endl;
cout << "done size = " << done.size() << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple = running_jobs.back();
running_jobs.pop_back();
lock.unlock();
ppa::Node *father = boost::get<0>(tuple);
ppa::Node *first_son = boost::get<1>(tuple);
ppa::Node *second_son = boost::get<2>(tuple);
bool flag = boost::get<3>(tuple);
father->start_alignment_new(&mf);
lock.lock();
cout << "done this align " << father->get_name() << " " << first_son->get_name() << " " << second_son->get_name()
<< endl;
cout << "setting the done tuple to 1 to get them next" << endl;
done.push_back(tuple);
cout << "new size of the done " << done.size() << endl;
lock.unlock();
}
else {
lock.unlock();
lock.lock();
cout << "getting next item to do " << wait.back() << endl;
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> run = wait.back();
running_jobs.push_back(run);
wait.pop_back();
lock.unlock();
}
}
}
}
catch (boost::lock_error& le)
{
cout << "error this " << le.what() << endl;
}
}
Main:
int main(int argc, char *argv[])
{
clock_t t_start=clock();
ppa::Node* parent;
ppa::Node* left;
ppa::Node* rigth;
ppa::Node* check_root;
/*
...
boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple = dups.back();
ppa::Node *n = boost::get<0>(tuple);
//cout << "creating waiting thread" << endl;
// g.create_thread( boost::bind(wait_function));
cout << "creating other threads" << endl;
for ( int i = 0; i < 5; ++i )
{
cout << "making thread " << i << endl;
g.create_thread( boost::bind( threaded_function, boost::ref(mf), boost::ref(n) ));
}
cout << g.size() << endl ;
// wait for them
// sleep(15);
g.join_all();
I'm pretty sure it comes form this part that is the new I have added.
Bad code, too many locks and unlocks, mutex has a lock method no need to use shared lock.lock()

How to detect first or last element iterating over a container?

How to do the following in more stylish/short way?
for(i=container.begin(); i!=container.end(); ++i) {
if (i!=container.begin()) {
cout << ", ";
}
cout << *i;
j=i;
if (++j==container.end()) {
cout << "!" << endl;
}
}
Solutions like foreach are acceptable (actions on first and last elements need to be configurable, though).
P.S.
There are many answers that are handling first element, but not last. Here is what I mean by handling last element:
for(i=container.begin(); i!=container.end(); ++i) {
j=i;
if (i==container.begin()) {
cout << "[" << *i << "]" << endl;
} else if (++j==container.end()) {
cout << ", (" << *i << ")" << "!" << endl;
} else {
cout << ", " << *i;
}
}
Don't you think it's very easy to handle first element outside the cycle body? The real problem is the last one! I'm sorry for not being able to clarify the important point asking the question. I think I'll just accept the top ranked answer eventually.
Boost has next / prior which can sometimes help in such situations.
for(i=container.begin(); i!=container.end(); ++i) {
if (boost::next(i) == container.end()) {
std::cout << "!" << std::endl;
}
}
Although for this specific case, I'd simply output the first element, loop from second till last while always outputting the ',' and then output the '!' after the loop has ended. (as others have suggested already)
I don't see the point in moving the special cases inside the loop, and then checking inside the loop for them....
My advice here would be: there is no point in detecting anything within this loop !
Since your special cases are at the beginning and the end of your container, it is easy to remove their processing from within the loop.
The following function will print the contents of any container class whose elements can be <<'ed to an std::ostream:
template < class Container >
void print(Container const & container)
{
typename Container::const_iterator current = container.begin();
typename Container::const_iterator const end = container.end();
if (current != end)
{
std::cout << *current;
for (++current; current != end; ++current)
{
std::cout << ", " << *current;
}
std::cout << "!" << std::endl;
}
}
In your code,
if (i==container.end()) {
cout << "!" << endl;
}
will never happen.
My own approach would be to use the container size (I think size() is now constant time for all Standard Library containers). Maintain a count in the loop and you are at the end when count == size() - 1, and at the beginning when count == 0, obviously.
As container is not defined by you, I used the simplest - vector
template <class T>
string vector_join( const vector<T>& v, const string& token ){
ostringstream result;
for (typename vector<T>::const_iterator i = v.begin(); i != v.end(); i++){
if (i != v.begin()) result << token;
result << *i;
}
return result.str();
}
//usage
cout << vector_join( container, ", " ) << "!";
Shift the ++i a bit:
i = container.begin();
while(i != container.end()) {
if (i != container.begin()) {
cout << ", ";
}
cout << *i;
if (++i == container.end()) {
cout << "!" << endl;
}
}
template < class TContainerType>
void print(TContainerType const & i_container)
{
typename TContainerTypeconst ::const_iterator current = i_container.begin();
typename TContainerTypeconst ::const_iterator const end = i_container.end();
if(current != end)
{
std::cout << *current++;
while(current != end)
std::cout << ", " << *current++;
}
std::cout << "!" << std::endl;
}
Take the second part out of the loop.
for(i=container.begin(); i!=container.end(); ++i) {
if (i != container.begin()) {
cout << ", ";
}
cout << *i;
}
cout << "!" << endl;