Thursday, 12 March 2015

C++ Strings creation and initialization with examples

C++ Strings creation and initialization with examples:


Creating and initializing strings is a direct scheme, and flexible as well.
I just want to tell the same this using the below example.
The first string, Str1, is declared but contains no initial value. Unlike a C char array, which would contain a random and worthless bit pattern until initialization, Str1 does contain valid information. This string object has been initialized to hold “no characters,” and can properly report its 0 length and absence of data elements through the use of class member functions.

The next string, Str2, is initialized by the literal argument "Where are my socks?". This form of initialization uses a quoted character array as a parameter to the string constructor. By contrast, Str3 is simply initialized with an assignment. The last string of the group, Str4, is initialized using an existing C++ string object.

See the below sample code to understand the initialization of the strings.
1. Create an empty string and defer initializing it with character data
2. Initialize a string by passing a literal, quoted character array as an argument to the constructor
3. Initialize a string using ‘=‘
4. Use one string to initialize with another string

#include "stdafx.h"
#include <string> #include <iostream> using namespace std; int _tmain (int argc, _TCHAR* argv[]) { string Str1; string Str2 ("This tis the first string"); string Str3 = "This i my second string"; string Str4 (Str3); getchar (); return 0; }
In C++ there are initialization of the strings is very easy

1. Use a portion of either a C char array or a C++ string
2. Combine different sources of initialization data using operator+
3. Use the string object’s substr( ) member function to create a substring

See the sample example below to initialize the strings:

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
string Str1 ("This is my first string to test");
string Str2 ("My second string to test with respect to the first string");
string Str3 ("This is my thried string");
string Str4 (Str1, 0, 8); // Copy the first 8 chars
string Str5 (Str2, 15, 6); // Copy 6 chars from the middle of the source
string Str6 (Str3, 6, 15); // Copy from middle to end
string strResult = Str4 + "and" + // Copy all sorts of stuff
Str1.substr(20, 10) + Str5 + // substr() copies 10 chars at element 20
"with" + Str3.substr(5, 100) + // substr() copies up to either 100 char or eos starting at element 5
Str1.substr(7, 1); // OK to copy a single char this way
cout << strResult << endl;
getchar ();
return 0;
}

substr( ) is one of  the strin member function,  takes a starting position as its first argument and the number of characters to select as the second argument. Both of these arguments have default values and if you say substr( ) with an empty argument list you produce a copy of the entire string, so this is a convenient way to duplicate a string.

Here’s what the string strResult contains the result.

Based on the above example, C++ allows string initialization techniques to be mixed in a single statement, a flexible and convenient feature.
string iterators  are the another string initialization technique string.begin( ) and string.end( ). This treats a string like a container object.which has iterators indicating the start and end of the “container.” This way you can hand a string constructor two iterators and it will copy from one to the other into the new string.

The below example explains usage of the string.begin( ) and string.end( ) functions.
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
string Str1 ("Test String");
string str2(Str1.begin(), Str1.end());
cout << str2 << endl;
getchar ();
return 0;
}

The iterators are not restricted to begin( ) and end( ), so you can choose a subset of characters from the source string.
C++ strings may not be initialized with single characters or with ASCII or other integer
values. This is one of the limitation using C++ strings. .

No comments: