processing every Character? Use range-based for
If we want to does something to every character in a string, by far the best approach are to use a statement introduced by th E New standard:the range for statement. This statement iterates through the elements in a given sequence and performs some operation on each value in that Sequenc E. The syntactic form is
forexpression) statement
Where expression is an object of a type that represents sequence, and declaration defines the variable that we'll use to a Ccess the underlying elements in the sequence. On all iteration, the variable in declaration are initialized from the value of the next element in expression.
A string represents a sequence of characters, so we can use a string as the expression in a range for. As a simple example, we can use the a range for the print each character from a string in its own line of output:
string str (" some string ");//print the characters in str one character to a linefor ( Auto c:str)//for every char in str cout<< C << Endl; Print the current character followed by a newline
The For Loop associates the variable C with Str. We define the loop control variable the same-on-the-same-and-variable. In this case, we have the compiler determine the type of C, which in this case would be char. On each iteration, the next character in Str is copied into C. Thus, we can read this loop as saying, ' for every character C in the string str, ' do something. The "Something" in this box is to print the character followed by a newline.
As a somewhat more complicated example, we'll use a range for and the INPUNCT function to count the number of punctuation Characters in a string:
string s ( "Hello world!!!" ); //punct_cnt have the same type that s.size returns decltype (S.size ()) punct_cnt= 0 ; //count the number of punctuation charaters in S for (auto c:s) //for every char in S if (ispunct (c)) //if the Character is punctuation ++punct_cnt; //increment the punctuation counter cout << punct_cnt << punctuation Characters in " << s << endl;
The output of this program is
3puctuationcharactersinHelloWorld!!!
Here we use Decltype (if you want + message, just go to there:the auto and decltype Type specifier) to declare our COU Nter.
Using s subscript for Random Access
The previous example we advanced our subscript one position at a time to capitalize each character in sequence. We can also calculate an subscript and directly fetch the indicated character. There is no need to access characters in sequence.
As an example, let's assume we have a number between 0 and we want to generate the hexadecimal representation of th at number. We can do so using a string, which is initialized to hold the hexadecimal "digits":
Const stringhexdigits="0123456789ABCDEF";//possible hex digitscout<<"Enter A series of numbers between 0 and"<<"separated by spaces." Hit ENTER when finished: "<< Endl;stringResult//would hold the resulting hexify ' d stringstring:: Size_type N;//Hold numbers from the input while(Cin>>n)if(N < hexdigits.size ())//Ignore Invalid inputresult+= Hexdigits[n];//Fetch the indicated hex digitcout<<"You are hex number is :"<< result << Endl;
If we give this program the input
12 0 5 15 8 15
The output would be
hexis: C05F8F
Whenever we use a subscript, we should think about what we know that it's in range. Subscript, N, is a string::size_type, which as we know are an unsigned type. As a result, we know that n are guaranteed to being greater than or equal to 0. Before we use N to subscript hexdigits, we verify that it's less than the size of hexdigits.
"C + + Considerations" 6 Library String Type