string and c_str C++
阿新 • • 發佈:2018-12-11
1 string (C++)
1.1 initialization
#include <iostream> #include <string> using namespace std; int main() { // string s1(); function declaration string s1; //default constructor string s2(s1); //copy constructor s2 = "abcdf"; string s3(s2, 2); //substring constructor cout<<s2<<" "<<s3<<endl; string s4("test"); //from c-string string s5("test", 2); //from sequence string s6(10, 'a'); //fill constructor string s7(s6.begin(), s6.end()); cout<<s1<<"\n"<<s2<<"\n"<<s3<<"\n"; cout<<s4<<"\n"<<s5<<"\n"<<s6<<"\n"; cout<<s7<<"\n"; return 0; }
1.2 size vs length vs capacity vs max_size
dynamic size during the runtime
size和length是相同的,可以互換使用 - how many characters actually exist in the string
capacity - the maximum number of characters that the string can currently hold without having to grow
max_size - the maximum length the string can reach
#include <iostream> #include <string> using namespace std; int main() { string str ("Test~"); cout << "size: " << str.size() << "\n"; cout << "length: " << str.length() << "\n"; cout << "capacity: " << str.capacity() << "\n"; cout << "max_size: " << str.max_size() << "\n"; return 0; }
string is not null-terminated so we can add '\0' into the string.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "ab\0\0cd"; // s1 contains "ab", using string literal
string s2{"ab\0\0cd", 4}; // s2 contains "ab\0\0cd", using different ctr
string s3 = "ab\0\0cd"s; // s3 contains "ab\0\0cd", using ""s operator (since C++14)
cout << s1 << endl;
cout << "size: " << s1.size() << "\n";
cout << "length: " << s1.length() << "\n";
cout << "capacity: " << s1.capacity() << "\n";
cout << "max_size: " << s1.max_size() << "\n\n";
cout << s2 << endl;
cout << "size: " << s2.size() << "\n";
cout << "length: " << s2.length() << "\n";
cout << "capacity: " << s2.capacity() << "\n";
cout << "max_size: " << s2.max_size() << "\n\n";
cout << s3 << endl;
cout << "size: " << s3.size() << "\n";
cout << "length: " << s3.length() << "\n";
cout << "capacity: " << s3.capacity() << "\n";
cout << "max_size: " << s3.max_size();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "this is a string~";
s1[2] = '\0';
string s2 = "\0also a string~";
cout<<"the length of s1 - ("<<s1<<") : "<<s1.size()<<endl;
cout<<"the length of s2 - ("<<s2<<") : "<<s2.size()<<endl;
return 0;
}
1.3 string -> c_str
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
string s1 = "string";
const char* s2 = s1.c_str(); // make sure null-termination
const char* s3 = s1.data();
char s4[20];
strcpy(s4, s1.c_str());
cout<<s1<<" "<<s1.size()<<" "<<sizeof(s1)<<endl;
cout<<s2<<" "<<strlen(s2)<<" "<<sizeof(s2)<<endl; // the size of a pointer
cout<<s3<<" "<<strlen(s3)<<" "<<sizeof(s3)<<endl; // the size of a pointer
cout<<s4<<" "<<strlen(s4)<<" "<<sizeof(s4)<<endl;
return 0;
}
2 char[] (C string)
2.1 initialization
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
char foo1[20]; // 必須要知道char array的size
char foo2[] = {}; // size=0
char foo3[] = {'a', 'b', 'c', '\0'}; // size=4
char foo4[] = ""; // size=1
char foo5[] = "abc"; // size=4
char foo6[20] = "a";
cout<<"foo1: ("<<foo1<<") "<<strlen(foo1)<<" "<<sizeof(foo1)<<endl; // strlen(search for null only) returns a undefined reault here
cout<<"foo2: ("<<foo2<<") "<<strlen(foo2)<<" "<<sizeof(foo2)<<endl;
cout<<"foo3: ("<<foo3<<") "<<strlen(foo3)<<" "<<sizeof(foo3)<<endl;
cout<<"foo4: ("<<foo4<<") "<<strlen(foo4)<<" "<<sizeof(foo4)<<endl;
cout<<"foo5: ("<<foo5<<") "<<strlen(foo5)<<" "<<sizeof(foo5)<<endl;
cout<<"foo6: ("<<foo6<<") "<<strlen(foo6)<<" "<<sizeof(foo6)<<endl;
return 0;
}
2.2 strlen vs sizeof
strlen - what's size of the string contain, not including null run-time
sizeof - what's the size of the container, including null compile time
2.3 char[] -> string
see below
3 char* (C string)
initialization
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
char foo[] = "abc";
char* fo = "abc";
string foo_str(foo);
string fo_str(fo);
cout<<foo_str<<" "<<foo_str.size()<<endl;
cout<<fo_str<<" "<<fo_str.size()<<endl;
return 0;
}