zoukankan      html  css  js  c++  java
  • string Type

    Notes from C++ Primer

    Operations

    Operations of string support lots of operations of sequential container.

    • string s;          define a new empty string object, named s.
    • string s(cp);    define a new string object, initialized by a C-style string pointed by cp.
    • string s(s2);    define a new string object initialized by the copy of s2.
    • is >> s;          read a string splited by space from input stream is, write into s.
    • os << s;         output the s into output stream os.
    • getline(is, s)    get a line of string from input stream is, output into s.
    • s1 + s2           concatenate string s1 and string s2.
    • s1 += s2        concatenate string s2 at the end of string s1.

    As the operation of string is almost the same as container, many executions of vector can be replaced like codes below:

    string s("Hiya!");
    string::iterator iter = s.begin();
    while(iter != s.end())
    	cout << *iter++ << endl;	// postfix increment: print old value
    

    Operations only for string

    There're three base operations supported by string type but container type.

    • substr function, return the sub-string of current string object.
    • append and replace function, modify string object.
    • a series of find function, which is used to find string object.

    Operations of sub-string:

    • s.substr(pos, n)    return a substring of s, from position pos with length n in s.
    • s.substr(pos)        return a substring of s, from position pos to the end of s.
    • s.substr()             return a copy of s.

    1. substr

    We can pass the beginning position pos of ing substring and a counter n which determines the length of substring to function substr to finish returning substring.

    string s("hello world");
    
    // return substring of 5 characters starting at position 6
    string s2 = s.substr(6, 5);		// s2 = world
    

    An alternative way is:

    // return substring from position 6 to the end of s
    string s3 = s.substr(6);		// s3 = world
    

    2. append and replace

    append function offers an shortcut to insert string at the end of string:

    string s("C++ Primer");		// initialize s to "C++ Primer"
    s.append(" 3rd Ed.");		// s == "C++ Primer 3rd Ed."
    
    // equivalent to s.append(" 3rd Ed.")
    s.insert(s.size(), " 3rd ED.");
    

    replace function is a shortcut of deleting some characters and then inserting other contents: 

    // starting at position 11, erase 3 characters and then insert "4th"
    s.replace(11, 3, "4th");	// s == "C++ Primer 4th Ed."
    
    // equivalent way to replace "3rd" by "4th"
    s.erase(11, 3);				// s == "C++ Primer Ed."
    s.insert(11, "4th");		// s == "C++ Primer 4th Ed."
    

    Also, we don't need to require the length of inserting string is the same as the length of deleting string. Thus we can replace longer or shorter string:

    s.replace(11, 3, "Fourth");		// s == "C++ Primer Fourth Ed."
    

    3. find operations of string

    string class offers 6 kinds of serarch function. They all return a string::size_type type value indicating the position of match, or return a special value string::npos indicating fail. string class define npos as a value larger than any validate index of string.

    • s.find(args)                      find the first position match with args in s.
    • s.rfind(args)                     find the last position match with args in s.
    • s.find_first_of(args)          find the first position match with any character in args in s.
    • s.find_last_of(args)           find the last position match with any character in args in s.
    • s.find_first_not_of(args)    find the first position of character not belong to args in s.
    • s.find_first_not_of(args)    find the first position of character not belong to args in s.

    3.1 The simple accurate search is find function.

    string name("AnnaBelle");
    string::size_type pos1 = name.find("Anna");		// pos1 == 0
    

    By default, find operation is case sensitive:

    string lowercase("annabelle");
    pos1 = lowercase.find("Anna");		// pos1 == npos
    

    3.2 Find any character

    The process of finding any character is more complicated. For example, find the first number in name:

    string numerics("0123456789");
    string name("r2d2");
    string::size_type pos = name.find_first_of(numerics);
    cout << "found number at index: " << pos
    	 << " element is " << name[pos] << endl;	// pos == 1
    

    3.3 Find from a predetermined position

    We can pass one more parameter pos to the find function, which indicates the beginning position of finding. Generally, this parameter is used to find all the match characters of string s in a loop.

    string::size_type pos = 0;
    
    // each trip reset pos to the next instance in name
    while((pos = name.find_first_of(numerics, pos)) != string::npos)
    {
    	cout << "found number at index: " << pos
    		 << " element is " << name[pos] << endl;
    	
    	++pos;		// move to the next character
    }
    

    3.4 Find the mismatch position

    find_first_not_of function is used to find the first position of mismatch character. For example, finding the first nonnumerical character:

    string numbers("0123456789");
    string dept("03714p3");
    
    // returns 5, which is the index to the character 'p'
    string::size_type pos = dep.find_first_not_of(numerics);
    

    3.5 Reverse finding

    We can also find the character from right to left:

    string river("Mississippi");
    string:size_type first_pos = river.find("is");		// return 1
    string:size_type last_pos = river.rfind("is");		// return 4
    

    Attention: find_last function is very likely the find_first function. The only difference is the return of find_last is the last position of first match substring, and the find_first returns the first position of first match match substring.

  • 相关阅读:
    更改hadoop native库文件后datanode故障
    解决 Unable to load native-hadoop library for your platform
    Windows 下Hadoop的环境变量配置
    在用VMware虚拟机的时候,有时会发现打开虚拟机时提示“该虚拟机似乎正在使用中。如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏。配置文件: D:win10x64Windows 10 x64.vmx。”这是由于虚拟机未正常关闭引起的,下面看看解决办法
    Linux中chown和chmod的区别和用法(转)
    linux jps 命令
    org.apache.hadoop.yarn.exceptions.InvalidAuxServiceException: The auxService: mapreduce_shuffle do
    静默安装oracle 11g,环境预检查时报错,SEVERE: [FATAL] PRVF-0002 : 无法检索本地节点名
    windows,cmd中,如何切换到磁盘的根目录下
    windows,cmd中查看当前目录下的文件及文件夹
  • 原文地址:https://www.cnblogs.com/kid551/p/4264788.html
Copyright © 2011-2022 走看看