Message from C, C++ discussions
January 2020
— I'm creating a priority queue of pair<int,string> which stores the elements based on the value of int and if they are equal then compare the string
Here is the code:
``
class cmp
{
public:
bool operator()(pair<int, string> &x, pair<int, string> &y)
{
if (x.first != y.first)
return x.first < y.first;
return x.second < y.second;
}
};
priority_queue< pair<int,string>, vector<pair<int,string>>, cmp> q;
q.push({1, "coding"});
q.push({2, "love"});
q.push({2, "i"});
q.push({1, "leetcode"});
while(!q.empty()){
pair<int,string> p = q.top();
q.pop();
cout<<p.second<<endl;
}
``
— Output:
love
i
leetcode
coding
— Why is "love" and "i" coming first in the output
— My comparator should put the element first which has smaller int and if that clashes then put the smaller string(lexicographically) first
— Hey does anyone know to properly interface a c++ program with Arduino via serial communication?? I have done it but it is not accurate the tx and rx pins blink when am sending data through the serial port but most of the time the Arduino doesn't do anything I programmed it to do!!
— Do you use correct band?
— Yes!
— Got it
— Why I didn't find on the internet the bubble sort algorithm fully optimized like this?
I implemented it but everywhere it's half-optimized, this should be fully optimized isn't it?
— The problem with this optimization is that if we have for example an array long 1000 and the last half [500-999] is already sorted, this algorithm will still check if it's sorted, ye?
instead with mine I put a wall where the last swap has taken place and I check all before that, am I right?
— Purge complete.