C + + string processing

Source: Internet
Author: User

#include <iostream>
#include <cstdio>
#include <string>
using namespace Std;
int main ()
{
String Str ("1 2 3 4 5 6 7 8");
Char ch[] = "ABCDEFGH";
String A;
String str_1 (CH);
String str_2 (str, 2, 5);
String Str_3 (CH, 5);
String Str_4 (5, ' X ');
String Str_5 (Str.begin (), Str.end ());
cout<< "Output digital sequence" <<endl;
cout<<str<<endl;
cout<< "output character sequence" <<endl;
cout<<ch<<endl;
cout<<a<<endl;

cout<<str_1<<endl;
cout<<str_2<<endl;
cout<<str_3<<endl;
cout<<str_4<<endl;
cout<<str_5<<endl;
return 0;

}



==========================================================

#include <iostream>
#include <cstdio>
#include <string>
using namespace Std;
int main ()
{
int size = 0;
int length = 0;
unsigned long maxsize = 0;
int capacity = 0;
String str ("12345678");
String Str_custom;
Pre-allocated storage size
Str_custom.reserve (5);
Str_custom = str;

To find the number of characters
Size = Str_custom.size ();
Length = Str_custom.length ();

Maximum capacity of a character
MaxSize = Str_custom.max_size ();


Maximum memory capacity that can be obtained before new memory is allocated
Capacity = Str_custom.capacity ();
cout<< "size =" <<size<<endl;
cout<< "length =" <<length<<endl;
cout<< "maxsize =" <<maxsize<<endl;
cout<< "Capacity =" <<capacity<<endl;
return 0;
}

========================================================

#include <iostream>
#include <cstdio>
#include < String>
using namespace std;
int main ()
{
string CS ("conststring");
string S ("ABCDE");
char temp = 0;
char temp_1 = 0;
char temp_2 = 0;
char temp_3 = 0;
char temp_4 = 0;
char temp_5 = 0;
temp =s[2];
temp_1 = s.at (2);
temp_2 = Cs[cs.length ()];
cout<<temp_1<<endl;
Cout<<temp_2<<endl;

return 0;
}

=========================================================


Comparison

#include <iostream>
#include <cstdio>
#include < String>
using namespace std;
int main ()
{
string A ("ABcdef");
string B ("AbcdEf");
string C ("123456");
string D ("123DFG");
int m = A.compare (B);
int n = a.compare (1, 5, B);
int p = a.compare (1, 5, B, 4, 2);
int q = C.compare (0, 3, D, 0, 3);
cout<< "m=" <<m<< "n=" <<n<< "p=" <<p << ", q=" <<q<<endl;
return 0;
}



C + + string processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.