Insertion in std::vector vs. insertion in std::deque - c++

I was solving the puzzles in 2017 Advent of Code. It was necessary to fill in circular buffer using certain algorithm. For the buffer implementation I first used vector, and then I tried with deque. I am getting different results when printing values of the vector and the queue. Here's the code:
#include <iostream>
#include <vector>
void PrintBuffer(std::vector<int> a_CircularBuffer)
{
for (std::vector<int>::iterator it = a_CircularBuffer.begin(); it != a_CircularBuffer.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> circularBuffer;
circularBuffer.reserve(20);
circularBuffer.push_back(0);
circularBuffer.push_back(1);
std::vector<int>::iterator currentPosition = circularBuffer.begin() + 1;
for (int i = 2; i < 20; ++i) {
int steps = 378;
if (steps >= i) {
steps = (steps % i);
}
if ((circularBuffer.end() - currentPosition) <= steps) {
currentPosition = circularBuffer.begin() + (((currentPosition - circularBuffer.begin()) + steps) % i);
circularBuffer.insert(currentPosition, i);
}
else {
currentPosition = currentPosition + steps;
circularBuffer.insert(currentPosition, i);
}
PrintBuffer(circularBuffer);
}
return 0;
}
This is the result when using vector:
0 2 1
0 3 2 1
0 3 2 4 1
0 5 3 2 4 1
0 6 5 3 2 4 1
0 7 6 5 3 2 4 1
0 7 6 8 5 3 2 4 1
0 7 6 9 8 5 3 2 4 1
0 10 7 6 9 8 5 3 2 4 1
0 10 7 6 9 11 8 5 3 2 4 1
0 10 7 6 9 11 8 5 3 2 4 12 1
0 10 7 6 9 11 8 5 3 2 4 12 13 1
0 10 7 6 9 11 8 5 3 2 4 12 14 13 1
15 0 10 7 6 9 11 8 5 3 2 4 12 14 13 1
...
and this is when using deque (just change "vector" to "deque" and comment out circularBuffer.reserve(20) line):
0 2 1
0 3 2 1
0 3 2 4 1
0 5 3 2 4 1
0 5 6 3 2 4 1
0 5 6 7 3 2 4 1
0 5 6 7 3 8 2 4 1
0 5 6 7 3 9 8 2 4 1
0 5 6 10 7 3 9 8 2 4 1
0 5 6 10 7 3 9 8 11 2 4 1
0 5 12 6 10 7 3 9 8 11 2 4 1
0 5 12 6 13 10 7 3 9 8 11 2 4 1
0 5 12 6 13 14 10 7 3 9 8 11 2 4 1
0 5 12 6 13 14 10 7 3 15 9 8 11 2 4 1
...
Why there are different results for vector and deque?

You get undefined behaviour when you insert an element causing reallocation, and then use the old iterator again.
Anything can happen.
Use index to store current position and it'll work the same way.

Related

My selection sort replaces some numbers in the array with 0

This is the complete code that I wrote:
( [numsin the function] and [arrin main] is the array that needs to be sorted, sizeis the amount of numbers in the array, minis the smallest number in the unsorted part)
#include <iostream>
#include <vector>
using namespace std;
void sort(vector <int> &nums, int size){
int min = 0;
for(int i=0;i<size;i++){
min = i;
for(int j=i+1;j<size;j++){
if(nums[j]<nums[min]){
min = j; //comparing
}
}
nums[i] = nums[min] + nums[i]; //swaping
nums[min] = nums[i] - nums[min];
nums[i] = nums[i] - nums[min];
}
}
int main(){
cout<<"\nEnter Numbers:\n";
vector <int> arr;
int num;
while(cin>>num){
arr.push_back(num);
}
sort(arr,arr.size());
cout<<"\nSorted:\n";
for(int i=0;i<arr.size();i++){
cout<<arr[i]<<" ";
}
}
I'm writing a code that simply sorts the given array. But after trying to debug and find solutions online, I can't figure out which part is wrong. These are some examples of my results:
Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z
Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z
Sorted:
2 3 4 5 0 7 0
Enter Numbers:
9 8 7 6 5 4 3 2 1 ^Z
Sorted:
1 2 3 4 0 0 0 0 0
This is the result when I added a for loop under the swapping part to show what every round has done to the array:
Enter Numbers:
9 8 7 6 1 2 3 4 5 ^Z
1 8 7 6 9 2 3 4 5
1 2 7 6 9 8 3 4 5
1 2 3 6 9 8 7 4 5
1 2 3 4 9 8 7 6 5
1 2 3 4 5 8 7 6 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 0 8 9
1 2 3 4 5 6 0 0 9
1 2 3 4 5 6 0 0 0
Sorted:
1 2 3 4 5 6 0 0 0
Enter Numbers:
6 4 8 7 2 3 5 ^Z
2 4 8 7 6 3 5
2 3 8 7 6 4 5
2 3 4 7 6 8 5
2 3 4 5 6 8 7
2 3 4 5 0 8 7
2 3 4 5 0 7 8
2 3 4 5 0 7 0
Sorted:
2 3 4 5 0 7 0
9 8 7 6 5 4 3 2 1 ^Z
1 8 7 6 5 4 3 2 9
1 2 7 6 5 4 3 8 9
1 2 3 6 5 4 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 0 6 7 8 9
1 2 3 4 0 0 7 8 9
1 2 3 4 0 0 0 8 9
1 2 3 4 0 0 0 0 9
1 2 3 4 0 0 0 0 0
Sorted:
1 2 3 4 0 0 0 0 0
Please help, thanks.
Your swapping logic doesn't handle the case where the smallest remaining element is the first unsorted element (i.e., i == min). Consider what each line does in this case:
nums[i] = nums[min] + nums[i]; //nums[i] will be doubled
nums[min] = nums[i] - nums[min]; // nums[min] is subtracted from itself, making it 0
nums[i] = nums[i] - nums[min]; // nums[i] is subtracted from itself gain, but 0-0 is still 0
The goal of avoiding a temporary isn't bad by itself, but you do have this nasty edge case. You'd either have to detect the edge case or just bite the bullet and deal with a temporary. You could also call std::swap, but that likely uses a temporary as well.
The advantage to using a temporary or std::swap is that this code would be easier to make generic for other types (especially via std::swap). In addition, std::swap can be specialized for types to avoid temporaries if possible, and if this is actually a bottleneck.

Loop a matrix and a vector

I have a vector with 2 elements a_destinations that represent the source and destination with two integers. 2 4 or 5 1...
I have a matrix test as follows:
0 15 15 15 13 13 15 15 13 13 15 15 15 13 15 15
2 1 2 2 2 2 2 2 2 2 10 2 2 2 10 2
6 1 2 3 3 3 6 6 6 6 6 6 6 6 6 6
7 2 2 3 4 4 2 7 7 7 7 7 7 7 7 7
8 3 3 3 4 5 3 3 8 8 3 3 3 8 3 3
9 4 4 4 4 5 4 4 4 9 4 4 4 9 4 4
7 2 2 2 2 2 6 7 7 7 11 11 7 7 11 7
12 6 6 3 3 3 6 7 8 8 12 12 12 12 12 12
9 7 7 7 4 4 7 7 8 9 7 7 7 9 7 7
13 8 8 8 8 5 8 8 8 9 13 13 13 13 13 13
14 1 11 11 11 11 11 11 11 11 10 11 11 11 14 14
12 6 6 12 12 12 6 12 12 12 10 11 12 12 10 12
15 7 7 7 7 7 7 7 7 13 11 11 12 13 11 15
0 12 12 12 9 9 12 12 9 9 12 12 12 13 12 12
15 10 10 10 10 10 10 10 10 10 10 10 10 10 14 15
0 12 12 12 12 12 12 12 12 12 14 12 12 12 14 15
where test[i][j] represents the path from i to j
each time test[i][j] != j we do the loop again
Example:
path from 2 to 4 >> test[2][4] = 3, test[3][4] = 4: we output: 2, 3, 4
path from 1 to 7 >> test[1][7] = 2, test[2][7] = 6 , test[6][7] = 7: we output 1, 2 ,6, 7
I tried as follows:
std::vector<vector<int>> test;
test = Graphe->P; // MATRIX IS FILLED like on top
vector< int > a_destinations; // Vector with the destinations: 2 4 or 5 10 or 1 4 ...
for ( unsigned i = 0; i < test.size(); i++){
for (unsigned j = 0; j< test.size(); j++){
for (unsigned k = 0; k < a_destinations->size() - 1 ; k ++){
if ( a_destinations->at(k) == i && a_destinations->at(k+1) == j ){
if (test[i][j] == a_destinations->at(k+1)){
cout << a_destinations->at(k) << ", " <<test[i][j];
} else {
cout << a_destinations->at(k) << ", " << test[a_destinations->at(k)][j];
}
cout << ", " << test[i][j];// << ", " << a_destinations->at(k+1);
}
}
}
}
But i end up with 2 destinations always.

Stripping off the zeros from a list containing a panda dataframe

i have a list
[0,0,0, DataFrame1,0,0,DataFrame2,0,0, DataFrame3]
where Dataframe is a "Panda Dataframe".
now what i am trying to do is to strip of the '0' zeros (being integers). Is there any way i can do this without using a loop. I tried to use set function, but it is not working with panda Dataframes.
My answer should resemble like this
[ DateFrame1, DataFrame2, DataFrame3]
As #Zero, suggest in comment.
l = []
df = pd.DataFrame(np.random.randint(0,10,(5,5))
l.append(0)
l.append(0)
l.append(df)
l.append(0)
l.append(0)
l.append(df)
print(l)
[0, 0, 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8, 0, 0, 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8]
[x for x in l if isinstance(x,pd.DataFrame)]
Output:
[ 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8, 0 1 2 3 4
0 5 4 9 6 7
1 9 9 2 7 3
2 4 9 4 8 3
3 4 6 2 5 5
4 8 1 2 1 8]

Why is srand(time(NULL)) working smoothly even though I repeatedly reset it?

I have a function that creates a vector of size N, and shuffles it:
void rand_vector_generator(int N) {
srand(time(NULL));
vector <int> perm(N);
for (unsigned k=0; k<N; k++) {
perm[k] = k;
}
random_shuffle(perm.begin(),perm.end());
}
I'm calling this from my main function with the loop:
for(int i=0; i<20; i++)
rand_vector_generator(10);
I expected this to not give me sufficient randomness in my shuffling because I'm calling srand(time(NULL)); with every function call and the seed is not too different from successive call to call. My understanding is that I call srand(time(NULL)); once and not multiple times so the seed doesn't "reset".
This thread somewhat affirms what I was expecting the result to be.
Instead, I get:
6 0 3 5 7 8 4 1 2 9
0 8 6 4 2 3 7 9 1 5
8 2 4 9 5 0 6 7 1 3
0 6 1 8 7 4 5 2 3 9
2 5 1 0 3 7 6 4 8 9
4 5 3 0 1 7 2 9 6 8
8 5 2 9 7 0 6 3 4 1
8 4 9 3 1 5 7 0 6 2
3 7 6 0 9 8 2 4 1 5
8 5 2 3 7 4 6 9 1 0
5 4 0 1 2 6 8 7 3 9
2 5 7 9 6 0 4 3 1 8
5 8 3 7 0 2 1 6 9 4
7 4 9 5 1 8 2 3 0 6
1 9 2 3 8 6 0 7 5 4
0 6 4 3 1 2 9 7 8 5
9 3 8 4 7 5 1 6 0 2
1 9 6 5 3 0 2 4 8 7
7 5 1 8 9 3 4 0 2 6
2 9 6 5 4 0 3 7 8 1
These vectors seem pretty randomly shuffled to me. What am I missing? Does the srand call somehow exist on a different scope than the function call so it doesn't get reset every call? Or am I misunderstanding something more fundamental here?
According to standard the use of std::rand in both std::random_shuffle and std::shuffle is implementation-defined (though it is often the case that an std::rand is used this is not guaranteed). Try it on another compiler? Another platform?
If you want to make sure the std::rand is used you should let your code use it explicitly (for example, using lambda expression):
random_shuffle(perm.begin(), perm.end(), []{return std::rand();});
On a somewhat unrelated note, the time()'s precision is one whole second, your code runs way faster than that (I would hope) so those multiple calls to srand() result in resetting to the same-ish seed

openCV: Plotting a colormap from a matrix

I have a 50 x 50 matrix having certain values from 1 to 50. I need to plot a color map for the same using OpenCV .
I have already done this in MATLAB (with help from this answer):
>> A = randi([10,60],100,100);
>> colormap('hot')
>> imagesc(A)
>> colorbar
And the output comes like .
I would like to do the same using openCV.
How do I proceed for this ?
I am unable to understand how do I implement the function of "hot" which appears like this (see only hot)
:
Additionally I would also be interested in putting a color bar as shown in the first image.
Update (Final code I am running)
Unfortunately still I am unable to plot the correct color map: Here is the MATLAB code:
Note : A is a 10 by 100 matrix.
A=[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6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6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
]
colormap('hot');
imagesc(A)
colorbar
And here is the output:
Now I created a text file with same data, and my text file looks like this:
And used the following code to achieve the same result as of MATLAB:
int main()
{
ifstream Read("myfile.txt");
vector<int> nums;
while ( !Read.eof() ) {
int n;
Read >> n;
nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat(nums);
cout<<mat<<endl; //for testing
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_JET );
// Show the result:
imshow("colormap", image);
waitKey(0);
return 0;
}
But the output from the above is meaningless.
I need to match the results from MATLAB and Opencv.
you will have to reshape the 1d vector, to a 2d mat :
//
// before doing anything else, CLEAN UP YOUR DAMN TXTFILE.
// it should contain nothing than numbers and spaces.
ifstream Read("m.txt");
// be extra picky about the type here.
// we are reading in a 8bit grayscale map.
vector<uchar> nums;
for (int i=0; (i<100*10)&&(!Read.eof()); i++ ) {
int n;
Read >> n;
nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
// your data is in the [0..9] range, so scale up to [0..255] uchar range
mat2d *= (255/10);
cout<<mat2d<<endl; //for testing
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_HOT );
// Show the result:
imshow("colormap", image);
waitKey(0);