CPP - Strings STL

when we pass the compare function in sort function is a parameter than only write compare not write in function calling formate ex -> compare(a,b)

i want to intersting to know how this funtion internally excuted

cin.get() and cin.getline both function give same output

Hello @khemchandrs,
1.
Sort function implicitly selects the arguments for compare function at runtime in the following manner.
Thus, we do not explicitly specify it’s parameters.

Suppose you have a collection of string and you have to sort them using compare function.
For input:
6
bat
apple
dat
hello
batman
hell

Output:
apple bat
dat apple
dat bat
hello apple
hello dat
batman apple
batman hello
batman dat
batman bat
batman apple
hell apple
hell hello
apple
batman
bat
dat
hello
hell

The following picture explains the pattern/ flow of element pass.

I have numbered the comparisons,
The order of number shows the order in which comparison is taking places,
In case of true, swapping occurs and I have shown the sequence after swapping.

image

Observations:

it sends the string in the right side in sequence as a and element at the left side in sequence as b.

Consider comparison (7) in the figure:
In case if compare returns true, it performs swap and take the element at left after swap as current element(i.e. a=batman) and compare it the element just before it(b=dat) and continues the swap until compare returns false.

Consider comparison (2),(4),(6),(11) in the figure:
In case if compare returns false, it makes the first element from the left which not yet traversed(i.e., a=dat or a=hello or a=batman or a=hell) as the current element(i.e a) and then
…compares it with the first element of the sequence
…compares it with the element just before it(i.e a).

Hope, this would help to a certain level.

Now answering your second question,

  1. cin
    It is used to read a word or character.
    It terminates on encountering whitespace or endline charater (’\n’)

  2. cin.get(char_array,number_of_characters,delimiter)
    To read characters including special characters like ’ ', ‘\n’ and ‘\t’.

  3. cin.getline(char_array,max_Size,delimiter)
    It is used to read a sentence or a paragraph.
    It terminates on encountering endline character.
    It also reads the whitespace.

Difference?

  1. The get() function is much like getline() but rather than read and discard the newline character, get() leaves that character in the input queue.

  2. get() leaves the delimiter in the queue thus letting you able to consider it as part of the next input.

Explanation:
cin.getline() reads input up to ‘\n’ and stops

cin.get() reads input up to ‘\n’ and keeps ‘\n’ in the stream

For example :

char str1[100];
char str2[100];
cin.getline(str1 , 100);
cin.get(str2 , 100);
cout << str1 << " "<<str2;

input :
1 2
3 4
output 1 2 3 4 // the output expexted

When reverse them
For example :

char str1[100];
char str2[100];
cin.get(str2 , 100);
cin.getline(str1 , 100);
cout << str1 << " "<<str2;

input :
1 2
3 4
output 1 2 // the output unexpexted because cin.getline() read the ‘\n’

Usage:
If you are talking about the newline character from a console input, it makes perfect sense to discard it, but if we consider input from a file, you can use as “delimiter” the beginning of the next field.

Hope, this would help.
Give a like, if you are satisfied.