Reputation:
I wrote the following program to print out the occurrence of an alphabet starting with a=0
, b=1
and so on. Could someone please point out why using std::distance is printing out a blank -
and how I could get rid of it?
// Example program
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string str;
cin>>str;
int n=str.size();
std::vector<char> table(26);
table = {
'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'
};
int i=0;
while(i<n) {
char c=str[i];
auto search = std::find(table.begin(), table.end(), c);
if(search!=table.end()) {
int dist = std::distance(search, table.begin());
cout<<dist; //prints out -0-1-2-3 instead of 0123, why?
}
i++;
}
return 0;
}
Working program is here
Upvotes: 2
Views: 2289
Reputation: 311146
The standard function std::distance
accepts a range of the kind [start, target]
. So you need to write
auto dist = std::distance( table.begin(), search );
Or without using the function std::distance
you can just write
auto dist = search - table.begin();
because the class template std::vector
has random access iterators.
A few words about the program.
It is the case when it is better to use C strings instead of standard containers like std::vector
and correspondingly to use standard C functions instead of C++ algorithms.
The program can look the following way
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
int main()
{
const char *table = "abcdefghijklmnopqrstuvwxyz";
std::string s;
std::getline( std::cin, s );
for ( char c : s )
{
c = std::tolower( ( unsigned char )c );
if ( const char *p = std::strchr( table, c ) )
{
std::cout << p - table << ' ';
}
}
std::cout << std::endl;
return 0;
}
If for example to enter
Umedh Singh Bundela
then the output will look like
20 12 4 3 7 18 8 13 6 7 1 20 13 3 4 11 0
Instead of the expression in the statement
std::cout << p - table << ' ';
you can use a call of std::distance
if you want provided that you'll include header <iterator>
std::cout << std::distance( table, p ) << ' ';
Upvotes: 0
Reputation: 238461
why using std::distance is printing out a blank
-
-
character is the minus sign. It is used to represent negative numbers. So, -
is printed because you print negative numbers. The distances are negative because the first argument is latter than the second one.
how I could get rid of it?
If you calculate the distance from earlier position to the latter, the distance will be positive:
std::distance(table.begin(), search);
In this case you do know, but in case you don't know or care about the order and only want the absolute distance, you can get it using the function std::abs
:
std::abs(std::distance(it1, it2));
PS. Passing latter iterator first is allowed only if the iterator is random access.
Upvotes: 3
Reputation: 169478
Because the distance between those two iterators is negative; you have them in the wrong order. The "lesser" iterator should be on the left, not the right.
You have: std::distance(search, table.begin())
You should have: std::distance(table.begin(), search)
Upvotes: 6