Appending a Vector to a Vector

Aug 7, 2015 at 11:53pm
Ok I know this is pretty basic but I can't seem to understand why this isn't working.
This is my test code:
1
2
3
test_(b2.to_string() == "01010100");
b2.erase(3);   // Having an issue inside erase function
test_(b2.to_string() == "0100100");


This is my erase function:
1
2
3
4
5
6
7
void erase(size_t pos, size_t nbits = 1) {
		BitArray<> front = slice(0, pos);
		BitArray<> back = slice(pos + nbits, (used_bits - (pos + nbits)));
		front += back;
		cout << front << endl;	//Test purposes: output is 0100 which is incorrect
		*this = front;
}

So is there something I'm forgetting here? By my understanding it should append 'back' to the end of 'front'. Thank you for helping me with this.
Last edited on Aug 8, 2015 at 2:12am
Aug 8, 2015 at 12:53am
¿are we suppossed to guess the implementation details of your class?

You say that `+=' should append but it does not, perhaps the problem is with its code.
Or perhaps it isn't receiving the right arguments, so blame `slice()' or your usage of it
Or the problem may be in your print algorithm.


Provide enough code to reproduce your problem.
Aug 8, 2015 at 2:11am
This is my slice()
1
2
3
4
5
6
7
8
BitArray slice(size_t pos, size_t count)const {
		BitArray<> b(count);
		for (int i = 0; i < count; i++)
		{
			b.assignBit(i, this->readBit(i + pos));
		}
		return b;
	}


This is my to_string()
1
2
3
4
5
6
7
8
9
10
	string to_string() const {
		string charStr = "";
		for (size_t i = 0; i < used_bits; i++) {
			if (test(i))
				charStr += '1';
			else
				charStr += '0';
		}
		return charStr;
	}


This is my test() considering it's being called in my to_string()
1
2
3
4
5
6
bool test(size_t pos)const {
		if (Bit_Vector.size() == 0)
			throw logic_error("Empty");
		else
			return Bit_Vector[pos / BITS_PER_WORD] & (1u << (pos%BITS_PER_WORD));
	}
Aug 8, 2015 at 2:36am
--Edited
Apparently line 4 of my second block of code in my first post:
 
front+=back;

is calling on this function
1
2
3
4
5
6
7
8
9
10
11
// Appends a BitArray Object
BitArray& operator+=(const BitArray& b)
	{
		if (capacity() == used_bits)
			Bit_Vector.resize(Bit_Vector.size() + Bit_Vector.size());
		for (size_t i = 0; i < used_bits; ++i) {
			copy(used_bits - 1, b[i]);
		}
		used_bits++;
		return *this;
	}
Last edited on Aug 8, 2015 at 4:38pm
Aug 8, 2015 at 3:25pm
http://www.eelis.net/iso-c++/testcase.xhtml
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.



1
2
3
4
5
6
7
8
9
10
11
// Appends a BitArray Object
BitArray& operator+=(const BitArray& b)
	{
		if (capacity() == used_bits) //¿you don't need to consider `b'?
			Bit_Vector.resize(Bit_Vector.size() + Bit_Vector.size()); //¿is this enough? ¿what if `b' is really big?
		for (size_t i = 0; i < used_bits; ++i) { //in the next line you are traversing `b', so b.used_bits
			copy(used_bits - 1, b[i]); //no idea what does this function do, ¿why the first argument remains constant?
		}
		used_bits++; //¿so you just added only one bit?
		return *this;
	}
Last edited on Aug 8, 2015 at 3:25pm
Aug 8, 2015 at 4:42pm
I'm trying to add the value of b, which in this case is '0100', to *this, which is the value of '010'. My end result is suppose to be '0100100' but I'm getting '0100' instead.
Aug 8, 2015 at 5:54pm
¿why is it so hard for you to provide your full code?


add b, which in this case is '0100', (4 bits)
to *this, which is the value of '010'. (3 bits)
result is suppose to be '0100100' (7 bits)
but I'm getting '0100' (4 bits)

I'm going to suppose that `used_bits' is the quantity of bits that you've got. (so this->used_bits is 3 and b.used_bits is 4)

In your code you do used_bits++; increasing in just 1 unit, but you need to increase it in 4
used_bits += b.used_bits; (however this invariance should be handled by your insert_at_the_end() function, which you've called copy())

In the loop you do for (size_t i = 0; i < used_bits/*3*/; ++i) when trying to add the bits of `b', you'll miss the last one.
Topic archived. No new replies allowed.