Tuesday, 12 June 2018

String C++


  • A character array is simply an array of characters can terminated by a null character. A string is a class which defines objects that be represented as stream of characters.
  • Implementation of character array is faster than std:: string. Strings are slowerwhen compared to implementation than character array.
Operations on strings
Input Functions
1. getline() :- This function is used to store a stream of characters as entered by the user in the object memory.
2. push_back() :- This function is used to input a character at the end of the string.
3. pop_back() :- Introduced from C++11(for strings), this function is used to delete the last character from the string.
// Declaring string
    string str;
 
    // Taking string input using getline()
      getline(cin,str);
 
    // Displaying string
    cout << "The initial string is : ";
    cout << str << endl;
 
    // Using push_back() to insert a character
    // at end
    // pushes 's' in this case
    str.push_back('s');
 
    // Displaying string
    cout << "The string after push_back operation is : ";
    cout << str << endl;
 
    // Using pop_back() to delete a character
    // from end
    // pops 's' in this case
    str.pop_back();
 

Capacity Functions
3. capacity() :- This function returns the capacity allocated to the string, which can be equal to or more than the size of the string. Additional space is allocated so that when the new characters are added to the string, the operations can be done efficiently.
4. resize() :- This function changes the size of string, the size can be increased or decreased.
5.shrink_to_fit() :- This function decreases the capacity of the string and makes it equal to its size. This operation is useful to save additional memory if we are sure that no further addition of characters have to be made.

// Initializing string
    string str = "geeksforgeeks is for geeks";
 
    // Displaying string
    cout << "The initial string is : ";
    cout << str << endl;
 
    // Resizing string using resize()
    str.resize(13);
 
    // Displaying string
    cout << "The string after resize operation is : ";
    cout << str << endl;
 
    // Displaying capacity of string
    cout << "The capacity of string is : ";
    cout << str.capacity() << endl;
 
    // Decreasing the capacity of string
    // using shrink_to_fit()
    str.shrink_to_fit();
 
    // Displaying string
    cout << "The new capacity after shrinking is : ";
    cout << str.capacity() << endl;
The initial string is : geeksforgeeks is for geeks
The string after resize operation is : geeksforgeeks
The capacity of string is : 26
The new capacity after shrinking is : 13

Iterator Functions
7. begin() :- This function returns an iterator to beginning of the string.
8. end() :- This function returns an iterator to end of the string.
9. rbegin() :- This function returns a reverse iterator pointing at the end of string.
10. rend() :- This function returns a reverse iterator pointing at beginning of string.
// Initializing string`
    string str = "geeksforgeeks";
 
    // Declaring iterator
    std::string::iterator it;
 
    // Declaring reverse iterator
    std::string::reverse_iterator it1;
 
    // Displaying string
    cout << "The string using forward iterators is : ";
    for (it=str.begin(); it!=str.end(); it++)
    cout << *it;
    cout << endl;
 
    // Displaying reverse string
    cout << "The reverse string using reverse iterators is : ";
    for (it1=str.rbegin(); it1!=str.rend(); it1++)
    cout << *it1;
    cout << endl;
Manipulating Functions
11. copy(“char array”, len, pos) :- This function copies the substring in target character array mentioned in its arguments. It takes 3 arguments, target char array, length to be copied and starting position in string to start copying.
12. swap() :- This function swaps one string with other.
// Initializing 1st string
    string str1 = "geeksforgeeks is for geeks";
 
    // Declaring 2nd string
    string str2 = "geeksforgeeks rocks";
 
    // Declaring character array
    char ch[80];
 
    // using copy() to copy elements into char array
    // copies "geeksforgeeks"
    str1.copy(ch,13,0);

/ using swap() to swap string content
    str1.swap(str2);
 
13.
Get character in string
Returns a reference to the character at position pos in the string.
// string::at
#include 
#include 

int main ()
{
  std::string str ("Test string");
  for (unsigned i=0; ireturn
0; }

14: Find content in string
std::string str ("There are two needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  found=str.find("needles are small",found+1,6);
  if (found!=std::string::npos)
    std::cout << "second 'needle' found at: " << found << '\n';

  found=str.find("haystack");
  if (found!=std::string::npos)
    std::cout << "'haystack' also found at: " << found << '\n';

  found=str.find('.');
  if (found!=std::string::npos)
    std::cout << "Period found at: " << found << '\n';
15:  Replace the string
// Using positions:                 0123456789*123456789*12345
  std::string str=base;           // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string." (1)
16:
Generate substring
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.

std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end
                                               live in details.




No comments:

Post a Comment