Standard Library
C Standard Library
The C standard library operates on character arrays char[]/const char*.
See: fprintf, fscanf, Null-terminated byte strings
printf("%s", s): Use%sto output a string (character array).scanf("%s", &s): Use%sto read a string (character array).sscanf(const char *__source, const char *__format, ...): Read variables from the string__source, e.g.,sscanf(str,"%d",&a).sprintf(char *__stream, const char *__format, ...): Output the content from the__formatstring to__stream, e.g.,sprintf(str,"%d",i).strlen(const char *str): Returns the number of characters fromstr[0]until'\0'. Note: without O2 optimization, using this in a loop condition has complexity \(\Theta(N)\).strcmp(const char *str1, const char *str2): Comparesstr1andstr2lexicographically. Returns a negative value ifstr1is lexicographically smaller,0if they are equal, and a positive value ifstr1is lexicographically larger. Note: do not assume the return values are only0,1,-1; across different platforms, the return values follow the sign convention but are not necessarily0,1,-1.strcpy(char *str, const char *src): Copies characters fromsrctostr.strandsrcare pointers to the beginning of character arrays. Returnsstrincluding the null terminator'\0'.strncpy(char *str, const char *src, int cnt): Copies at mostcntcharacters tostr. Ifsrcends beforecntcharacters are copied, null characters are written tostruntil a total ofcntcharacters are written.strcat(char *str1, const char *str2): Appendsstr2to the end ofstr1, replacing the'\0'at the end ofstr1with*str2. Returnsstr1.strstr(char *str1, const char *str2): Ifstr2is a substring ofstr1, returns the address of the first occurrence ofstr2instr1; ifstr2is not a substring, returnsNULL.strchr(const char *str, int c): Finds the first occurrence of charactercin stringstrand returns its address. ReturnsNULLif the character is not found.strrchr(const char *str, int c): Finds the last occurrence of charactercin stringstrand returns its address. ReturnsNULLif the character is not found.
C++ Standard Library
The C++ standard library operates on string objects std::string while also providing compatibility with character arrays.
See: std::basic_string, std::basic_string_view
- Overloads the addition operator
+. When both sides of+are of typestring/char/char[]/const char*, it concatenates the two variables and returns the resulting string (string). - The assignment operator
=can haveconst string/string/const char*/char*on the right-hand side. - The access operator
[cur]returns a reference to the character at positioncur. - The access functions
data()/c_str()return aconst char*pointer with the same content as thestring. - The capacity function
size()returns the number of characters in the string. find(ch, start = 0)searches and returns the position of characterchstarting fromstart;rfind(ch)searches from the end and returns the position of the first found characterch(both indices start from0) (returns-1if not found).substr(start, len)extracts a substring of lengthlenstarting from positionstart(0-indexed) (iflenis omitted, it extracts to the end of the string).append(s)appendssto the end of the string.append(s, pos, n)appendsncharacters from strings, starting from positionpos, to the end of the current string.replace(pos, n, s)deletesncharacters starting frompos, then inserts stringsatpos.erase(pos, n)deletesncharacters starting frompos.insert(pos, s)inserts stringsat positionpos.std::stringoverloads comparison logical operators; the complexity is \(\Theta(N)\).
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用