How to print Point (openCV) value? [duplicate] - c++

This question already has answers here:
No console output on cout
(7 answers)
Closed 5 years ago.
Is this possible to print Point value?
For the example, I have a Point like this:
Point coordinate = Point(150,300);
And I want to show its value. I tried several ways, like:
first:
cout << coordinate << "\n";
second:
cout << coordinate.x << coordinate.y << "\n";
I also try the suggestion to flush it, become:
std::cout << coordinate << std::endl;
But none of those are work in my case. Is there any suggestion? Thanks for your help.
Ps. I work with opencv 3 and c++

std::cout << coordinate.x << "," << coordinate.y << std::endl;

Related

CPP how to format strings to align to the right side [duplicate]

This question already has answers here:
C++ alignment when printing cout <<
(7 answers)
How can I easily format my data table in C++?
(8 answers)
Align columns output c++
(2 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I am looking for a simple CPP solution to align a string to the right size.
What do I mean by this?
here is an example:
the normal output would be
hello [message]
foo [message]
I would like it to output like this:
hello [message]
foo [message]
note: this is not for aligning a table but rather to know how to align a message by adding certain spaces to the left (or smth).
Like this ?
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(10) << "Hello" << " [message]" << std::endl ;
std::cout << std::setw(10) << "foo" << " [message]" << std::endl ;
}
In the library, you can find std:right and std:setw which can help to justify O/P to the right.
If 'i' is your string, then below is a snippet that you can use :-
#include <iomanip>
cout << right
<< setw(20)
<< fixed
<< i << endl;

Is it necessary to check range in bit representation C++ [duplicate]

This question already has an answer here:
`std::bitset` with and without boundary checks
(1 answer)
Closed 1 year ago.
Some data are stored in a 64 bit integer. As you can see, inside the getDrvAns function I check if the bit in the position drvIdx-1 is 0 or 1. One cannot be sure if the drvIdx will have a value in the right range (1-64). However, I noticed that if we put a value higher that 64, we have a wrap-around effect as demonstrated in the code below.
My question is, is this a standard behavior? Is it safe to leave it as it is without boundary checks?
Note: If the bit is set to 0 the drive has answered.
uint64_t mDrvAns = 48822;
bool getDrvAns(int drvIdx) {
std::bitset<64> bitRepOfmDrvAns{mDrvAns};
return (bitRepOfmDrvAns[drvIdx - 1] != 0ull);
};
std::string yesOrNo(bool val) {
return ((val==false) ? "Yes" : "No");
}
int main()
{
int drvIdx = 1;
std::bitset<64> bitsOfDrvAns{mDrvAns};
std::cout << bitsOfDrvAns << std::endl;
std::cout << "Has drv " << drvIdx << " answered? " << yesOrNo(getDrvAns(drvIdx))
<< std::endl;
drvIdx = 65;
std::cout << "Has drv " << drvIdx << " answered? " << yesOrNo(getDrvAns(drvIdx))
<< std::endl;
return 0;
}
According to the documentation, out of bounds access using operator[] is Undefined Behaviour. Don't do it.
If you don't want to check the bounds yourself, call test() instead, and be prepared to handle the exception if necessary.

map.end() error "iterator not dereferenceable" c++ [duplicate]

This question already has answers here:
iterators can't access problems properly
(3 answers)
Closed 5 years ago.
//Works
cout << "map[0] value is " << doubleStatsMap.begin()->first<< endl;
//gives error
cout << "map[last value is " << doubleStatsMap.end()->first << endl;
Im simply trying to get the value of the last element of my map. It works correctly with
"map.begin->first"
but is giving "map/set iterator not dereferencable" for the
"map.end()->first".
It cant be empty as the map has a beginning thus it has an end. Everything I've read says this should work. Any suggestions greatly appreciated!
Trying to get anything from the end iterator causes undefined behavior.
To get the last item, you can use std::map::rbegin().
// cout << "map[last value is " << doubleStatsMap.end()->first << endl;
cout << "map[last value is " << doubleStatsMap.rbegin()->first << endl;

cout does not print even with time delay [duplicate]

This question already has answers here:
std::cout won't print
(4 answers)
Closed 2 years ago.
I expect cout to print "hello" and two seconds later " world".
int t = time( NULL );
std::cout << "hello";
while( time(NULL) < (t + 2) );
std::cout << " world";
But instead, cout prints noting to screen until after two seconds later, then the program prints "hello world". Even if the time delay increases like (t + 9), it is the same result. I Am not familiar with this cout behaviour.
But if I add std::endl at the first cout like so:
std::cout << "hello" << std::endl;
...
I get the expected result "hello" and two seconds later " world ".
std::cout is usually buffered, meaning it may not output immediately unless you force it to. Try std::flush after your first output:
std::cout << "hello " << std::flush;

compare two vectors of string (character) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to compare two vectors string
vector <string> morse ={"A.-","B-...","C-.-.", "D-..", "E.", "F..-.", "G--.", "H....", "I.." ,"J.---", "K-.-", "L.-..", "M--" ,"N-." ,"O---" ,"P.--.", "Q--.-", "R.-.", "S...", "T-", "U..-", "V...-", "W.--" ,"X-..-" ,"Y-.--", "Z--.."};
vector<string> codeMorse (1);
codeMorse ={".---.--.-.-.-.---...-.---."};
if (morse[i][j]==codeMorse[k]){ //my problem here =error
}
Can anybody help me?
Your code has 2 problems:
You can't make 2-Dimensional vector that way nor you even tried to make it 2D.
You wrote morse[i][j] without previous definition of i and j.
To fix issue 1 & 2:
Include
#include <vector>
Make a vector of std::pair(s):
std::vector<std::pair<std::string, std::string>> morse;
That allows you to have a pair of strings.
To add a new morse code, use this:
morse.push_back(std::pair<std::string, std::string>("LETTER HERE", "MORSE CODE HERE"));
To read 'em use this:
//read all via loop
for (int i = 0; i <= morse.size(); i++) {
std::cout << "Letter: " << morse[i].first << std::endl; //.first access your first elemt of the pair
std::cout << "Morse Code: " << morse[i].second << std::endl; //.second to access the morse code
}
OR use iterators if you already know them:
//read all via loop
for (auto i = morse.begin(); i != morse.end(); i++) {
std::cout << "Letter: " << i->first << std::endl; //->first access your first elemt of the pair
std::cout << "Morse Code: " << i->second << std::endl; //->second to access the morse code
}
Of course you can read specific values:
std::cout << morse[0].first << std::endl; //[] same use as the array's brackets
std::cout << morse[0].second << std::endl; //same here