C/C++文字作成処理
12603 ワード
C:
strncpy(a,b,5);
a[5]='0';
char a[10];
memset(a,'#',sizeof(a));
a[10]='0';
1.
strlen(st);
2.
strcmp(st1,st2);
strncmp(st1,st2,n); st1,st2 n 。
3.
strcat(st1,st2);
strncat(st1,st2,n); n st2 n st1, '0'。
4.
strcpy(st1,st2);
strncpy(st1,st2,n); n st2 n st1, '0'。
5.
where = strchr(st,ch) ch 。
where = strspn(st1,st2); 。
where = strstr(st1,st2);
C++:
<string>
string str;
1.
len = str.length();
len = str.size();
2.
:
str1.compare(str2);
str1.compare(pos1,len1,str2,pos2,len2); ,0 , 。
nops 。
3.
str1 += str2;
str1.append(str2);
str1.append(str2.pos2,len2);
4.
str2 = str1.substr();
str2 = str1.substr(pos1);
str2 = str1.substr(pos1,len1);
string a=s.substr(0,4); // s 0 4
5.
where = str1.find(str2);
where = str1.find(str2,pos1); pos1 str1 。
where = str1.rfind(str2); 。
6.
。
str1.insert(pos1,str2);
str1.insert(pos1,str2,pos2,len2);
str1.insert(pos1,numchar,char); numchar ,char
。
7.
str1.replace(pos1,str2);
str1.replace(pos1,str2,pos2,len2);
8.
str.erase(pos,len)
str.clear();
9.
swap(str1,str2);
10. C --> C++
char *cstr = "Hello";
string str1;
cstr = cstr;
string str2(cstr);
ACMer ,C C++ 、 , C 。
C++ string
string :
string(const char *s); // c s
string(int n,char c); // n c
,string , string s1;string
s2="hello"; 。 string
length_error
string :
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[] at() n , at ,
out_of_range , [] 。
const char *data()const;// null c
const char *c_str()const;// null c
int copy(char *s, int n, int pos = 0) const;// pos n
s ,
string :
int capacity()const; // ( string
)
int max_size()const; // string
int size()const; //
int length()const; //
bool empty()const; //
void resize(int len,char c);// len, c
string :
string operator>> // , operator<<
。
getline(istream &in,string &s);// in s ,
'n' 。
string :
string &operator=(const string &s);// s
string &assign(const char *s);// c s
string &assign(const char *s,int n);// c s n
string &assign(const string &s);// s
string &assign(int n,char c);// n c
string &assign(const string &s,int start,int n);// s start
n
string &assign(const_iterator first,const_itertor last);// first last
string :
string &operator+=(const string &s);// s
string &append(const char *s); // c s
string &append(const char *s,int n);// c s n
string &append(const string &s); // operator+=()
string &append(const string &s,int pos,int n); // s pos n
string &append(int n,char c); // n c
string &append(const_iterator first,const_iterator last);// first
last
string :
bool perator==(const string &s1,const string &s2)const;//
">","<",">=","<=","!=" ;
int compare(const string &s) const;// s
int compare(int pos, int n,const string &s)const;// pos
n s
int compare(int pos, int n,const string &s,int pos2,int n2)const;//
pos n s pos2 n2
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare > 1,< -1,== 0
string :
string substr(int pos = 0,int n = npos) const;// pos n
string :
void swap(string &s2); // s2
string :
int find(char c, int pos = 0) const;// pos c
int find(const char *s, int pos = 0) const;// pos s
int find(const char *s, int pos, int n) const;// pos s n
int find(const string &s, int pos = 0) const;// pos s
// , string::npos
int rfind(char c, int pos = npos) const;// pos c
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
// pos s n ,
, string::npos
int find_first_of(char c, int pos = 0) const;// pos c
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
// pos s n 。
string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
// s , string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of find_last_not_of find_first_of find_first_not_of ,
string :
string &replace(int p0, int n0,const char *s);// p0 n0 ,
p0 s
string &replace(int p0, int n0,const char *s, int n);// p0 n0
, p0 s n
string &replace(int p0, int n0,const string &s);// p0 n0 ,
p0 s
string &replace(int p0, int n0,const string &s, int pos, int n);// p0
n0 , p0 s pos n
string &replace(int p0, int n0,int n, char c);// p0 n0 ,
p0 n c
string &replace(iterator first0, iterator last0,const char *s);// [first0
,last0) s
string &replace(iterator first0, iterator last0,const char *s, int n);//
[first0,last0) s n
string &replace(iterator first0, iterator last0,const string &s);//
[first0,last0) s
string &replace(iterator first0, iterator last0,int n, char c);// [first0
,last0) n c
string &replace(iterator first0, iterator last0,const_iterator first,
const_iteratorlast);// [first0,last0) [first,last)
string :
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
// 4 p0 s pos n
string &insert(int p0, int n, char c);// p0 n c
iterator insert(iterator it, char c);// it c,
void insert(iterator it, const_iterator first, const_iterator last);// it
[first,last)
void insert(iterator it, int n, char c);// it n c
string
iterator erase(iterator first, iterator last);// [first,last)
,
iterator erase(iterator it);// it ,
string &erase(int pos = 0, int n = npos);// pos n ,
string :
string iterator,
, , 。
string::iterator string::const_iterator ,const_iterator
。 :
const_iterator begin()const;
iterator begin(); // string
const_iterator end()const;
iterator end(); // string
const_iterator rbegin()const;
iterator rbegin(); // string
const_iterator rend()const;
iterator rend(); // string rbegin
rend ,
string::reverse_iterator,string::const_reverse_iterator
:
ostringstream istringstream ,<sstream>
:
string input("hello,this is a test");
istringstream is(input);
string s1,s2,s3,s4;
is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
ostringstream os;
os<<s1<<s2<<s3<<s4;
cout<<os.str();