C++

Objectives :

  • File Name: string_find_first_of1.cpp
    • The function of C++ string class, string::find_firstof, finds a first occurrence of characters in a string, and not found in preceeding sequence.
    • The function of C/cstdio library, strchr(chstr1, 'e'), finds the first occurrence of  a character in a string;
    • Also see : compare_find_memchr1.htm, which searches  for the first occurrence of a character in an array.
       
  • Debug Step Over : in Quincy use F8 and watch window
     
    Step Into F7 Steps into the next statement, where cursor will screen (line of code) a statement. Executes the next statement or instruction, entering C or C++ functions or assembler subroutines.
    Step Over F8 Executes the next statement, function call, or instruction, without entering C or C++ functions or assembler subroutines.
    Step Out Function Steps out of the current function.  Useful if you want to exit a loop.
    Run to Cursor (F10) Runs the program and then pauses at the line containing the cursor set-point. 
  • Code: string_find_first_of1.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step: 3 Runtime Views: strchr() function of stdlocate the first occurrence of character of  string and prints the target point an onwards.

Step: Brief discussion : strchr() locates the first character in a string

Checking on "str2.find_first_of("X-Ray")" and str.find() functions :

Debug with F8 step-over: I have set a break point at a variable declaration, chptr1 and decided to watch other two variables. The purpose of this example remain focused to our objectives to understand difference between a string and character sequence.

As you would note that, string object str2 refers to a function npos; meaning npos is a static member constant of "string class" that determines the greatest possible value for an element of type size_t. Here, size_t is an unsigned integer meaning, it will get the positive values, using this grammer " strLength1 =str2.find("First");". You will note that reference "npos" and assigned values are referenced during the debugging steps.

Reference : ( http://www.cplusplus.com/reference/string/string/npos/ ) " This constant is actually defined with a value of -1 (for any trait), which because size_t is an unsigned integral type, becomes the largest possible representable value for this type."
 

At line number 14, the integer variable becomes "n1:=-1", which proves the above reference statement.

Note the match was found at 11th position, and the length of that matched string was 5 character long.

The illustration below shows the value of n1 which was expected show the beginning of ,string like"X-Ray" or "X-string"

Now note, the value of n1 changed with our request.

Checking on strchr() function:

Conclusion : This example shows the uses of string::find(), string:: find_first_of("X-man") and chptr1 = strchr(chstr1, 'e'). Using together we can get to the string or to an individual character, and it is not the end of the road. There are many ways to accomplish the above task.

 

Further Reading: Constructor of find_first_of(----) member function of the class size_t

  • constructor type :
    • size_type find_first_of( const basic_string& str, size_type pos = 0 ) const;

 

 

 

 

Finds the first character equal to one of characters in the given character sequence. Search begins at pos, i.e. the found character must not be in position preceding pos.

1) Finds the first character equal to one of characters in str.

2) Finds the first character equal to one of characters in the first count characters of the character string pointed to by s. s can include null characters.

3) Finds the first character equal to one of characters in character string pointed to by s. The length of the string is determined by the first null character.

4) Finds the first character equal to ch.