string::compare

1. compare 

string (1)
4int compare (const string& str) const noexcept;
substrings (2)
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str,
             size_t subpos, size_t sublen) const;
c-string (3)
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;

#include <string>
#include <iostream>
using namespace std;
int main()
{
size_t pos = 7;
size_t len = 3;

string s1("she is zhh");
string s2("zhh");
string s3("i love zhh");
const char *s4 = "zhh";
const char *s5 = "i love zhh";
cout << s1.compare(s2) << endl;
cout << s1.compare(pos, len, s2) << endl;
cout << s1.compare(pos ,len, s3, pos, len) << endl;
cout << s2.compare(s4) << endl;
cout << s1.compare(pos, len, s4) << endl;
cout << s1.compare(pos, len, s4, len) << endl;//最后一个参数莫名奇妙???
try
{
s1.compare(20, 100, s2);
}
catch(out_of_range)
{
cout << "out of range" << endl;
}
cout << "no exception" << endl;
return 0;
}

猜你喜欢

转载自www.cnblogs.com/xpylovely/p/12069122.html