C++ Template Library Module26 Tenouk
C++ Template Library Module26 Tenouk
Note:
This is continuation from the previous Module. Program examples in this Module compiled using Visual C++ 6.0
with SP6 and Visual C++ .Net. Some program examples may generate warning and runtime errors caused by
buffer/stack overflow. These good compilers have some protection for errors :o). g++ (run on Fedora 3 machine)
examples, given at the end of this Module.
Abilities
▪ Able to understand and use string template classes of the <string> in manipulating character
and string in C++.
▪ Able to understand the functionalities string template classes of the <string> in manipulating
character and string in C++.
▪ Able to appreciate the usefulness and use these string template classes in your own programs.
find_last_not_of()
- The return value is the index of the first character of the substring searched for when successful;
otherwise npos.
//find_last_not_of() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
//searching for a single character in a string
string str1("daddy donkey is dead");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index2 = str1.find_last_not_of('d');
cout<<"\nOperation: str1.find_last_not_of('d')"<<endl;
if(index2 != npos)
cout<<"The index of the non 'd' found in str1 is: "
<<unsigned int(index2)<<endl;
else
cout<<"The Character 'non d' was not found in str1."<<endl;
cout<<endl;
//----------------------------------------------------------
//searching a string for a substring as specified by a C-string
string str2("Testing Testing Testing");
cout<<"str2 string is: "<<str2<<"\n";
basic_string <char>::size_type index3, index4;
www.tenouk.com Page 1 of 30
<<"\n found in str2 before the 12th position."<<endl;
Output:
//find_last_not_of() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
//searching a string for a substring as specified by a C-string
string str3("Playing Testing Boring");
cout<<"str3 string is: "<<str3<<"\n";
basic_string <char>::size_type index5, index6;
static const basic_string <char>::size_type npos = -1;
//-----------------------------------------------------------
//searching a string for a substring as specified by a string
string str4("Testing 123 Testing 123");
cout<<"str4 string is: "<<str4<<"\n";
basic_string <char>::size_type index7, index8;
www.tenouk.com Page 2 of 30
string str5("3 1");
index7 = str4.find_last_not_of(str5, 18);
cout<<"Operation: str4.find_last_not_of(str5, 18)"<<endl;
if(index7 != npos)
cout<<"The index of the last occurrence of an "
<<"element not\nin '3 1' in str4 before the 18th "
<<"position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements other than those in the substring"
<<" '3 1' were not found in the string str4"<<endl;
string str6("Testing");
index8 = str4.find_last_not_of(str6);
cout<<"\nOperation: str4.find_last_not_of(str6)"<<endl;
if(index8 != npos)
cout<<"The index of the last occurrence of an "
<<"element not in\n'Testing' in str4 before the end "
<<"position is: "<<unsigned int(index8)<<endl;
else
cout<<"Elements other than those in the substring\n"
<<"'Testing' were not found in the string str4"<<endl;
return 0;
}
Output:
find_last_of()
- The return value is the index of the last character of the substring searched for when successful;
otherwise npos.
//find_last_of() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
//searching for a single character in a string
string str1("Testing 1234 Testing 1234");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index2 = str1.find_first_of('z');
cout<<"\nOperation: index2 = str1.find_first_of('z')"<<endl;
if(index2 != npos)
www.tenouk.com Page 3 of 30
cout<<"The index of the 'z' found in str1 is: "
<<unsigned int(index2)<<endl;
else
cout<<"The character 'z' was not found in str1"<<endl;
cout<<endl;
//--------------------------------------------------
//searching a string for a substring as specified by a C-string
string str2("Testing 1234 Testing 1234");
cout<<"str2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
Output:
//find_last_of() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
//searching a string for a substring as specified by a C-string
string str3("Testing 1234 Testing 1234");
cout<<"str3 string is: "<<str3<<endl;
basic_string <char>::size_type index5;
static const basic_string <char>::size_type npos = -1;
www.tenouk.com Page 4 of 30
else
cout<<"Elements of the substring 's1' were not\n"
<<"found in str3 before the 20th position."<<endl;
cout<<endl;
//-------------------------------------------------------
//searching a string for a substring as specified by a string
string str4("Testing 1234 Testing 1234");
cout<<"str4 string is: "<<str4<<endl;
basic_string <char>::size_type index6, index7;
string str5("416");
index6 = str4.find_last_of(str5, 25);
cout<<"Operation: str4.find_last_of(str5, 25)"<<endl;
if(index6 != npos)
cout<<"The index of the last occurrence of an "
<<"element\nof '416' in str4 before the 25th "
<<"position is: "<<unsigned int(index6)<<endl;
else
cout<<"Elements of the substring '416' were not\n"
<<"found in str4 after the 0th position"<<endl;
string str6("1g");
index7 = str4.find_last_of(str6);
cout<<"\nOperation: str4.find_last_of(str6)"<<endl;
if(index7 != npos)
cout<<"The index of the last occurrence of an "
<<"element\nof '1g' in str4 before the 0th "
<<"position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements of the substring '1g' were not\n"
<<"found in str4 after the 0th position"<< endl;
return 0;
}
Output:
get_allocator()
//get_allocator()
#include <string>
#include <iostream>
using namespace std;
int main()
{
//using the default allocator.
string str1;
basic_string <char> str2;
basic_string <char, char_traits<char>, allocator<char> > str3;
www.tenouk.com Page 5 of 30
basic_string <char> str4(str1.get_allocator());
insert()
- The return value is either a reference to the string object that is being assigned new characters by the
member function or, in the case of individual character insertions, an iterator addressing the position of
the character inserted, or none, depending on the particular member function.
//insert() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
//inserting a C-string at a given position
basic_string <char> str1("e insert() testing");
const char *cstr1 = "Th";
cout<<"str1 = "<<str1<<endl;
cout<<"cstr1 = "<<cstr1<<endl;
str1.insert(0, cstr1);
cout<<"Operation: str1.insert(0, cstr1)"<<endl;
cout<<"Inserting a C-string at position 0 is:\n"<<str1<<endl;
cout<<endl;
cout<<"str2 = "<<str2<<endl;
cout<<"cstr2 = "<<cstr2<<endl;
str2.insert(4, cstr2, 15);
cout<<"Operation: str2.insert(4, cstr2, 15)"<<endl;
cout<<"Inserting a C-string at the end is:\n"<<str2<<endl;
cout<<endl;
cout<<"str3 = "<<str3<<endl;
cout<<"str4 = "<<str4<<endl;
str3.insert(0, str4);
cout<<"Operation: str3.insert(0, str4)"<<endl;
cout<<"Inserting string at position 0 is:\n"<<str3<<endl;
cout<<endl;
cout<<"str5 = "<<str5<<endl;
cout<<"str6 = "<<str6<<endl;
str5.insert(7, str6, 4, 9);
cout<<"Operation: str5.insert(7, str6, 4, 9)"<<endl;
cout<<"Inserting part of a string at position 9 is:\n"<<str5<<endl;
return 0;
}
Output:
www.tenouk.com Page 6 of 30
//insert() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
//inserting a number of characters at a specified position in the string
string str7("Testing the insert()?");
cout<<"str7 = "<<str7<<endl;
str7.insert(20, 4, '!');
cout<<"Operation: str7.insert(20, 4, '!')"<<endl;
cout<<"Inserting characters: \n"<<str7<<endl;
cout<<endl;
Output:
www.tenouk.com Page 7 of 30
push_back()
//push_back()
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1("Testing the push_back()");
basic_string <char>::iterator StrIter, Str1Iter;
Output:
www.tenouk.com Page 8 of 30
- rbegin() is used with a reversed string just as begin is used with a string.
- If the return value of rbegin() is assigned to a const_reverse_iterator, the string object
cannot be modified. If the return value of rbegin() is assigned to a reverse_iterator, the
string object can be modified.
- rbegin() can be used to initialize an iteration through a string backwards.
- The return value of the rbegin() is a random-access iterator to the first element in a reversed string,
addressing what would be the last element in the corresponding un-reversed string.
- rend() is used with a reversed string just as end is used with a string.
- If the return value of rend() is assigned to a const_reverse_iterator, the string object
cannot be modified. If the return value of rend() is assigned to a reverse_iterator, the string
object can be modified.
- rend()can be used to test whether a reverse iterator has reached the end of its string.
- The return value of the rend() is a reverse random-access iterator that addresses the location
succeeding the last element in a reversed string. The value returned by rend()should not be
dereferenced.
int main()
{
string str1("The reverse begin, rbegin()"), str2;
basic_string <char>::reverse_iterator StrIter, Str1Iter;
basic_string <char>::const_reverse_iterator str1_rcIter;
Output:
www.tenouk.com Page 9 of 30
replace()
- The return value is the operand string with the replacement made.
//replace() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
Output:
www.tenouk.com Page 10 of 30
//replace() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout<<"Operation: str13.begin()"<<endl;
cout<<"Operation: str13.begin() + 3"<<endl;
cout<<"Operation: str13.replace(Iter1, Iter2, str14)"<<endl;
Iter1 = str13.begin();
Iter2 = str13.begin() + 3;
str11 = str13.replace(Iter1, Iter2, str14);
cout<<"The new string is: "<<str11<<endl;
cout<<"Operation: str13.replace(Iter1, Iter2, cstr3)"<<endl;
str12 = str13.replace(Iter1, Iter2, cstr3);
cout<<"The new string is: "<<str12<<"\n\n";
cout<<"Operation: str16.begin()"<<endl;
cout<<"Operation: str16.begin() + 4"<<endl;
cout<<"Operation: str16.replace(Iter3, Iter4, cstr4, 4)"<<endl;
Iter3 = str16.begin();
Iter4 = str16.begin() + 4;
str15 = str16.replace(Iter3, Iter4, cstr4, 4);
cout<<"The new string is: "<<str15<<"\n\n";
www.tenouk.com Page 11 of 30
string str17;
string str18("TESTING3");
char cstr5 = 'u';
Iter7 = str20.begin() + 1;
Iter8 = str20.begin() + 3;
Iter9 = str21.begin();
Iter10 = str21.begin() + 2;
cout<<"Operation: str20.replace(Iter7, Iter8, Iter9, Iter10)"<<endl;
str19 = str20.replace(Iter7, Iter8, Iter9, Iter10);
cout<<"The new string is: "<<str19<<endl;
return 0;
}
Output:
reserve()
www.tenouk.com Page 12 of 30
- Having sufficient capacity is important because reallocations is a time-consuming process and
invalidates all references, pointers, and iterators that refer to characters in a string.
- Unlike vector (another STL), the member function reserve() may be called to shrink the
capacity of an object. The request is non binding and may or may not happen.
- The default value for the parameter is zero, a call of reserve() is a non-binding request to shrink the
capacity of the string to fit the number of characters currently in the string.
- The capacity is never reduced below the current number of characters.
//reserve()
#include <string>
#include <iostream>
using namespace std;
int main()
{
Output:
www.tenouk.com Page 13 of 30
resize() and size()
- If the resulting size exceeds the maximum number of characters, the form throws length_error
exception handler.
int main()
{
SizeStr1 = str1.size();
CapaStr1 = str1.capacity();
SizeStr1 = str1.size();
CapaStr1 = str1.capacity();
SizeStr1 = str1.size();
CapaStr1 = str1.capacity();
Output:
www.tenouk.com Page 14 of 30
rfind()
- The return value is the index of the last occurrence when searched backwards, of the first character of
the substring when successful; otherwise npos.
int main()
{
//searching for a single character in a string
string str1("Testing the rfind() 1..2..3");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
cout<<"\nOperation: str1.rfind('z')"<<endl;
index2 = str1.rfind('z');
if(index2 != npos)
cout<<"The index of the 'z' found in str1 is: "<<index2<<endl;
else
cout<<"The character 'z' was not found in str1."<<endl;
cout<<endl;
//----------------------------------------------------------------
//searching a string for a substring as specified by a C-string
string str2("Testing the rfind() 123");
cout<<"The str2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
www.tenouk.com Page 15 of 30
<<"before\n the 25th position in str3 is: "<<index4<<endl;
else
cout<<"The substring 'nofind()' was not found in str2."<<endl;
return 0;
}
Output:
int main()
{
//searching a string for a substring as specified by a C-string
string str3("Another test. Testing the rfind() the 123");
cout<<"The str3 string is: "<<str3<<endl;
static const basic_string <char>::size_type npos = -1;
basic_string <char>::size_type index5, index6;
//----------------------------------------------------------
//searching string for a substring as specified by a string
string str4("Final rfind() testing 1...2...3");
cout<<"The str4 string is: "<<str4<<endl;
basic_string <char>::size_type index7, index8;
string str5("2...3");
cout<<"Operation: str4.rfind(str5, 30)"<<endl;
index7 = str4.rfind(str5, 30);
if(index7 != npos)
cout<<"The index of the 1st element of '1...2' "
<<"before\nthe 30th position in str4 is: "<<index7<<endl;
else
cout<<"The substring '1...2' was not found in str4\n"
<<"before the 30th position."<<endl;
string str6("...3");
cout<<"\nOperation: str4.rfind(str6)"<<endl;
www.tenouk.com Page 16 of 30
index8 = str4.rfind(str6);
if(index8 != npos)
cout<<"The index of the 1st element of '...3' in str4 is: "<<index8<<endl;
else
cout<<"The substring '...3' was not found in str4."<<endl;
return 0;
}
Output:
substr()
- The return value is a substring object that is a copy of elements of the string operand beginning at the
position specified by the first argument.
//substr()
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout<<"\nOperation: str1.substr()"<<endl;
basic_string <char> str3 = str1.substr();
cout<<"The default str3 substring is: "<<str3
<<"\nwhich is the original string."<<endl;
return 0;
}
Output:
www.tenouk.com Page 17 of 30
- The char_traits class describes attributes associated with a character. The template class
structure for char_traits is shown below.
- Where:
- The template class describes various character traits for type CharType. The template class
basic_string as well as several iostream template classes, including basic_ios, use this
information to manipulate elements of type CharType.
- Such an element type must not require explicit construction or destruction. It must supply a default
constructor, a copy constructor, and an assignment operator, with the expected semantics.
- A bitwise copy must have the same effect as an assignment. None of the member functions of class
char_traits can throw exceptions.
- The following table is a list of the char_traits class template typedef, the synonym name.
- The following table is a list of the char_traits class template member function.
www.tenouk.com Page 18 of 30
Note:
The following program example may generate a runtime error regarding the buffer overflow, because there are no
explicit exception handling code used in the program, the exceptions should be ‘fully’ handled by the compiler.
Good compiler should warn us regarding the exceptions. If the problem persists, try changing some of the pointer
variables to arrays variables as shown in move() program example. It should be OK.
assign()
//char_traits, assign()
#include <string>
#include <iostream>
using namespace std;
int main()
{
//assigning a character value to another character
char chr1 = 'P';
const char chr2 = 'Q';
Output:
compare()
- The comparison between the strings is made element by element, first testing for equality and then, if a
pair of elements in the sequence tests not equal, they are tested for less than.
- If two strings compare equal over a range but one is longer than the other, then the shorter of the two is
less than the longer one.
- The return value is a negative value if the first string is less than the second string, 0 if the two strings
are equal, or a positive value if the first string is greater than the second string.
//char_traits, compare()
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type* str1 = "TEST";
char_traits<char>::char_type* str2 = "RETEST";
char_traits<char>::char_type* str3 = "RETEST";
char_traits<char>::char_type* str4 = "TESTING";
www.tenouk.com Page 19 of 30
cout<<"str3 string is: "<<str3<<endl;
cout<<"str4 string is: "<<str4<<endl;
cout<<"\nOperation: Comparison..."<<endl;
int comp1, comp2, comp3, comp4;
comp1 = char_traits<char>::compare(str1, str2, 2);
comp2 = char_traits<char>::compare(str2, str3, 3);
comp3 = char_traits<char>::compare(str3, str4, 4);
comp4 = char_traits<char>::compare(str4, str3, 4);
cout<<"compare(str1, str2, 2) = "<<comp1<<endl;
cout<<"compare(str2, str3, 3) = "<<comp2<<endl;
cout<<"compare(str3, str4, 4) = "<<comp3<<endl;
cout<<"compare(str4, str3, 4) = "<<comp4<<endl;
return 0;
}
Output:
Note:
The following program example may generate a runtime error regarding the buffer overflow, because there are no
explicit exception handling code use in the program, the exceptions should be ‘fully’ handled by the compiler.
Good compiler should warn us regarding the exceptions. If the problem persists, try changing some of the pointer
variables to arrays variables as shown in move() program example. It should be OK.
copy()
- The source and destination character sequences must not overlap. Compare with the move() member
function.
//char_traits, copy()
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type* str1 = "Testing the copy()";
char_traits<char>::char_type* str2 = "Fucking";
char_traits<char>::char_type* result;
Output:
www.tenouk.com Page 20 of 30
eof()
- The return value is a value that represents end of file character (such as EOF or WEOF).
- If the value is represented as type char_type, it must correspond to no valid value of that type.
//char_traits, eof()
#include <string>
#include <iostream>
using namespace std;
int main( )
{
char_traits <char>::int_type int0 = char_traits<char>::eof();
cout<<"The eof return is: "<<int0<<endl;
Output:
eq()
- The return value is true if the first character is equal to the second character; otherwise false.
//char_traits, eq()
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type chr1 = 'P';
char_traits<char>::char_type chr2 = 'Q';
char_traits<char>::char_type chr3 = 'P';
//alternatively...
cout<<"\nOperation: using \'==\' operator, chr1==chr3"<<endl;
if(chr1 == chr3)
cout<<"The character chr1 and chr3 is equal."<<endl;
else
cout<<"The character chr1 and chr3 is not equal."<<endl;
www.tenouk.com Page 21 of 30
return 0;
}
Output:
eq_int_type()
- The return value is true if the first character is equal to the second character; otherwise false.
//char_traits, eq_int_type()
//and to_int_type()
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type chr1 = 'P';
char_traits<char>::char_type chr2 = 'Q';
char_traits<char>::char_type chr3 = 'P';
char_traits<char>::char_type chr4 = 'r';
cout<<"Operation: to_int_type(character)"<<endl;
cout<<"The char_types and corresponding int_types are:\n";
cout<<chr1<<" = "<<int1<<endl;
cout<<chr2<<" = "<<int2<<endl;
cout<<chr4<<" = "<<int4<<endl;
//alternatively...
cout<<"\nOperation: int1 == int3"<<endl;
if(int1 == int3)
cout<<"The int_type representation of characters chr1\n"
<<"and chr3 is equal."<<endl;
else
cout<<"The int_type representation of characters chr1\n"
<<"and chr3 is not equal."<<endl;
return 0;
}
Output:
www.tenouk.com Page 22 of 30
find()
//char_traits, find()
#include <string>
#include <iostream>
using namespace std;
int main( )
{
const char* str = "Testing the char_traits, find()";
const char* result1;
cout<<"The string to be searched is:\n"<<str<<endl;
Output:
length()
- The return value is the number of elements in the sequence being measured, not including the null
terminator.
//char_traits, length()
#include <string>
#include <iostream>
using namespace std;
www.tenouk.com Page 23 of 30
int main()
{
const char* str1= "Testing 1...2...3";
cout<<"str1 C-string is: "<<str1<<endl;
size_t LenStr1;
cout<<"\nOperation: length(str1)"<<endl;
LenStr1 = char_traits<char>::length(str1);
cout<<"The length of str1 is: "<<unsigned int(LenStr1)<<endl;
return 0;
}
Output:
lt()
- The return value is true if the first character is less than the second character; otherwise false.
//char_traits, lt()
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type chr1 = '1';
char_traits<char>::char_type chr2 = 'q';
char_traits<char>::char_type chr3 = 'R';
Output:
www.tenouk.com Page 24 of 30
move()
int main()
{
char_traits<char>::char_type str1[25] = "The Hell Boy";
char_traits<char>::char_type str2[25] = "Something To ponder";
char_traits<char>::char_type *result1;
Output:
not_eof()
- The return value is the int_type representation of the character tested, if the int_type of the
character is not equal to that of the EOF character.
- If the character int_type value is equal to the EOF int_type value, then it is false.
//char_traits, not_eof()
www.tenouk.com Page 25 of 30
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type chr1 = 'w';
char_traits<char>::int_type int1;
int1 = char_traits<char>::to_int_type(chr1);
cout<<"Operation: to_int_type(chr1)"<<endl;
cout<<"The char_type "<<chr1<<" = int_type "<<int1<<endl;
//EOF
char_traits <char>::int_type int2 = char_traits<char>::eof();
cout<<"\nOperation: char_traits<char>::eof()"<<endl;
cout<<"The eof return is: "<<int2<<endl;
eofTest2 = char_traits<char>::not_eof(int2);
cout<<"\nOperation: not_eof(int2)"<<endl;
if(!eofTest2)
cout<<"The eofTest2 indicates "<<chr1<<" is an EOF character."<<endl;
else
cout<<"The eofTest1 returns: "<<eofTest2
<<", which is the character "
<<char_traits<char>::to_char_type(eofTest2)<<endl;
return 0;
}
Output:
- The conversion operations to_int_type and to_char_type are inverse operation to each other.
For example:
to_int_type(to_char_type(x)) == x
- The return value is the char_type character corresponding to the int_type character.
- A value that cannot be represented by the conversion will yield an unspecified result.
//char_traits, to_char_type(),
//to_int_type and eq()
www.tenouk.com Page 26 of 30
#include <string>
#include <iostream>
using namespace std;
int main()
{
char_traits<char>::char_type chr1 = '3';
char_traits<char>::char_type chr2 = 'C';
char_traits<char>::char_type chr3 = '#';
cout<<"Operation: to_int_type(character)"<<endl;
cout<<"The char_types and corresponding int_types are:\n";
cout<<chr1<<" ==> "<<int1<<endl;
cout<<chr2<<" ==> "<<int2<<endl;
cout<<chr3<<" ==> "<<int3<<endl;
cout<<"\nOperation: to_char_type(integer)"<<endl;
cout<<"The inverse conversion are:\n";
cout<<int1<<" ==> "<<rev_chr1<<endl;
cout<<int2<<" ==> "<<rev_chr2<<endl;
//alternatively...
if(rev_chr2 == chr2)
cout<<"The rev_chr2 is equal to the original chr2."<<endl;
else
cout<<"The rev_chr2 is not equal to the original chr2."<<endl;
return 0;
}
Output:
www.tenouk.com Page 27 of 30
A class that is a specialization of the template class
char_traits<char>
char_traits<CharType> to an element of type
class
char.
A class that is a specialization of the template class
char_traits<wchar_t>
char_traits<CharType> to an element of type
class
wchar_t.
- The following are recompiling and re running of the C characters and strings program examples.
Program examples are taken from Module X. You can try other program examples as well.
//strtok()
//using the C++ wrappers
#include <cstdio>
#include <string>
using namespace std;
int main()
{
char string[] = "Is this sentence has 6 tokens?";
char *tokenPtr;
Output:
//Using strspn()
#include <cstdio>
#include <string>
using namespace std;
int main()
{
char *string1 = "The initial value is 3.14159";
char *string2 = "aehilsTuv";
www.tenouk.com Page 28 of 30
printf("string2 = %s\n", string2);
printf("\nThe length of the initial segment of string1\n");
printf("containing only characters from string2 is = %u\n", strspn(string1,
string2));
return 0;
}
Output:
- Program example compiled using g++. Portability is not an issue here :o). g++ warmly warn you for
constructs that are obsolete!
//**********string2.cpp************
//insert() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
//inserting a C-string at a given position
basic_string <char> str1("e insert() testing");
const char *cstr1 = "Th";
cout<<"str1 = "<<str1<<endl;
cout<<"cstr1 = "<<cstr1<<endl;
str1.insert(0, cstr1);
cout<<"Operation: str1.insert(0, cstr1)"<<endl;
cout<<"Inserting a C-string at position 0 is:\n"<<str1<<endl;
cout<<endl;
cout<<"str2 = "<<str2<<endl;
cout<<"cstr2 = "<<cstr2<<endl;
str2.insert(4, cstr2, 15);
cout<<"Operation: str2.insert(4, cstr2, 15)"<<endl;
cout<<"Inserting a C-string at the end is:\n"<<str2<<endl;
cout<<endl;
cout<<"str3 = "<<str3<<endl;
cout<<"str4 = "<<str4<<endl;
str3.insert(0, str4);
cout<<"Operation: str3.insert(0, str4)"<<endl;
cout<<"Inserting string at position 0 is:\n"<<str3<<endl;
cout<<endl;
cout<<"str5 = "<<str5<<endl;
cout<<"str6 = "<<str6<<endl;
str5.insert(7, str6, 4, 9);
cout<<"Operation: str5.insert(7, str6, 4, 9)"<<endl;
cout<<"Inserting part of a string at position 9 is:\n"<<str5<<endl;
return 0;
}
www.tenouk.com Page 29 of 30
[bodo@bakawali ~]$ g++ string2.cpp -o string2
[bodo@bakawali ~]$ ./string2
str2 = Test
cstr2 = ing an insert()
Operation: str2.insert(4, cstr2, 15)
Inserting a C-string at the end is:
Testing an insert()
str5 = Testing
str6 = the insert()
Operation: str5.insert(7, str6, 4, 9)
Inserting part of a string at position 9 is:
Testing insert()
----------------------------------o0o----------------------------------------
---www.tenouk.com---
www.tenouk.com Page 30 of 30