C + + string usage detailed __c++

Source: Internet
Author: User
Tags string find apache log

Turn from: https://www.byvoid.com/blog/cpp-string


///////////////////////////////////////////////////////////////////////////////////

C + + language is a very good language, but excellence does not mean perfect. There are still many people who are unwilling to use C or C + + and why. One of the reasons for this is that the text-processing function is too cumbersome and inconvenient to use. I've never been in any other language before, and when people say that, I always dismiss the idea that they don't understand the essence of C + +, or that they don't know C + +, and now that I've been in touch with Perl, PHP, and shell scripts, I've come to understand why some people say that C +

For example, if the text format is: User name phone number, filename name.txt Tom 23245332 Jenny 22231231 heny 22183942 Tom 23245332 ...

Now we need to sort the username and output only a different name.

So in shell programming, you can use this:

awk ' {print $} ' name.txt | Sort | Uniq

It's easy.

If you are in trouble with C + +, he needs to do the following: Open the file first, detect whether the file is open, and if it fails, exit. Declares a large enough array of two-dimensional characters or an array of character pointers to read a line to a character space and then parse a line of structure, find a space, and deposit it in a character array. Close the file to write a sort function, or use the Write a comparison function, use sort () sorting to traverse the array, compare if there is the same, if there is, then delete, copy ... Output information

You can use C + + or C language to achieve this process. If a person's main job is to handle this kind of text (such as doing Apache log stats and analytics), do you think he would like C + +?

Of course, with the STL, these processes can be greatly simplified. We can use FStream to replace the troublesome fopen fread fclose, using vectors instead of arrays. The most important thing is to use a string instead of a char * array, sort by using the sort sort algorithm, and then use the unique function to go heavy. Sounds like a good idea. Look at the following code (routine 1):

#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
int main ()
{
    ifstream in ("Name.txt");
    string strtmp;
    Vector<string> Vect;
    while (Getline (in, strtmp, ' \ n '))
    Vect.push_back (strtmp.substr (0, Strtmp.find ("))");
    Sort (Vect.begin (), Vect.end ());
    Vector<string>::iterator It=unique (Vect.begin (), Vect.end ());
    Copy (Vect.begin (), it, ostream_iterator<string> (cout, "\ n"));
    return 0;
}

It's not bad, at least it's a lot simpler than you think. (There is no processing of errors in the code, just to illustrate the problem, do not follow).

Of course, in this text format, using a map without vectors is more scalable, for example, by looking for a phone number for a person's name, and so on, but using a map doesn't work like that. You can try it with map.

Here string works not only to store strings, but also to provide comparisons of strings, lookups, and more. The less and EQUAL_TO functions are used by default in the sort and unique functions, and a section of the above code actually uses the following functions of string: storage function, Lookup function in Getline () function, neutron string function in Find () function, in substr () String operator < in the function, by default calling string operator = = in the sort () function, by default in the unique () function

In short, with a string, C + + character text processing function has finally been a certain complement, coupled with the use of other STL container, its text processing function has been with Perl, Shell, PHP, the distance is much reduced. So mastering string will make your work easier.

1 string uses

In fact, string is not a separate container, just a typedef of the Basic_string template class, and the corresponding wstring, you will find the following code in the string header file:

extern "C + +" {
typedef basic_string <char> string;
typedef basic_string <wchar_t> wstring;
} extern "C + +"

This article does not distinguish between string and basic_string, because it simply explains the use of string and without special instructions.

A string actually acts as a sequence container for saving characters, so in addition to some common operations with strings, there are operations that contain all the sequence containers. Common operations for strings include: Add, delete, modify, find comparisons, link, input, output, and more. A list of detailed functions is listed in the appendix. Do not be afraid of so many functions, in fact, there are many sequences of containers with, usually not necessarily used on.

If you want to know the detailed usage of all the functions, you need to check the basic_string, or download the STL programming manual. Some common functions are introduced here through examples.

1.1 Full use of the string operator

String overloads a number of operators, including +, + +, +, =,, [], <<, >>, and so on, which is very convenient for string manipulation. Let's take a look at the following example:

#include <string> #include <iostream> using namespace std;
    int main () {string strinfo= "Please input your name:";
    cout << Strinfo;
    Cin >> Strinfo; if (Strinfo = = "Winter") cout << "You are winter!"
    <<endl; else if (strinfo!= "Wende") cout << "You are not wende!"
    <<endl;
    else if (Strinfo < "winter") cout << "Your name should be ahead of winter" <<endl;
    else cout << "Your name should is after of winter" <<endl;
    Strinfo + = ", Welcome to china!";
    cout << strinfo<<endl;
    cout << "Your name is:" <<endl;
    String strtmp = "How are" + strinfo;
    for (int i = 0; i < strtmp.size (); i + +) cout<<strtmp[i];
return 0; }

The following is the output of the program

Please input your Name:hero your
are not wende!
Hero, Welcome to china!
How are? Hero, Welcome to china!

With these operators, affine functions can be used directly as parameters in STL, such as less, great, equal_to, and so on, so when you pass a string as a parameter, its use is no different from int or float. For example, you can use:

Map<string, int> Mymap; The above default uses the Less<string>

With operator +, you can directly add, for example:

String strinfo= "Winter";
String strlast= "Hello" + Strinfo + "!";
String strtest= "Hello" + strinfo + "Welcome" + "to" + "!";/ /You can also do this:

See the characteristics of it. As long as you have a string object in your equation, you can keep "+" all the time, but one thing you need to be sure of is that there must be a string object in the first two items. The principle is simple:

The system encountered a "+" number and found that one of the items was a string object. The system converts another item into a temporary string object. Performs a operator + operation that returns a new temporary string object. If you find the "+" number again, proceed to the first step.

Since this equation is performed from left to right, if both of the first two items are const char, the program itself does not define the addition of two const chars, and it must be a problem when compiling.

With the operator, assign (), append (), compare (), at (), etc. functions, unless there are some special requirements, generally not used. Of course the at () function also has a function, that is, check the subscript is legal, if you are using:

String str= "Winter";//the following line may cause a program interrupt error
str[100]= '! '; /The following will throw an exception: Throws:out_of_range
cout<<str.at <<endl;

Do you understand. If you want to be efficient, or use [] to access, if you want stability, it is best to use at () to access.

1.2 Dazzling string Find function

Because lookup is one of the most frequently used features, String provides a very rich lookup function. The list is as follows: Function name Description Find lookup rfind Reverse lookup find_first_of find any character in a substring, return the first position find_first_not_of find any character that does not contain a substring, return to the first position find_last_of check Find any character in a substring, return to the last position find_last_not_of lookup does not contain any characters in the substring, the last position is returned the function is overloaded 4 times, the following is an example of the Find_first_of function to illustrate their parameters, other functions and their parameters , which means there are a total of 24 functions:

Size_type find_first_of (const basic_string& s, size_type pos = 0)
size_type find_first_of (const chart* S, size_ty PE pos, size_type N)
size_type find_first_of (const chart* s, size_type pos = 0)
size_type find_first_of (CharT C, s Ize_type pos = 0)

All lookup functions return a size_type type, which is generally the position of the found string, and returns String::npos if it is not found. It is important to note that all comparisons with String::npos must be made using string::size_type and not directly using types such as int or unsigned int. In fact, String::npos says-1, look at the header file:

Template <class _chart, class _traits, class _alloc>
const BASIC_STRING<_CHART,_TRAITS,_ALLOC>::SIZE_ Type
Basic_string<_chart,_traits,_alloc>::npos
= basic_string<_chart,_traits,_alloc>::size_ Type)-1;

Find and RFind are also relatively easy to understand, one is a forward match, one is a reverse match, the following parameter POS is used to specify the starting lookup location. For find_first_of and find_last_of are not so well understood.

Find_first_of is given a character set to look up, and finds the first position in the string of any character in the character set. Maybe it's easier to see an example.

There is a need to filter all non-English characters at the beginning and end of a line. See how to do this with string:

#include <string>
#include <iostream>
using namespace std;
int main ()
{
    string strinfo= "//*---Hello Word!......------";
    String strset= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    INT-A-strinfo.find_first_of (strset);
    if (A/= String::npos)
    {cout<< "not find any
        characters" <<endl;
        return-1;
    }
    int last = strinfo.find_last_of (strset);
    if (last = = String::npos)
    {cout<< "not find any
        characters" <<endl;
        return-1;
    }
    cout << Strinfo.substr (Last-first + 1) <<endl;
    return 0;
}

Here, write all the letters in English. In order to find the character set, first find the position of the first letter, then find the position of the last English letter, then use the substr to the middle part of the output results. Here's the result:

Hello Word

The preceding symbol and the following symbol are gone. This usage can be used to find the delimiter, thus dividing a contiguous string into sections to use awk in the shell command. Especially when there are multiple delimiters, you can specify one at a time. For example, there is a need for:

John |3456123, Hunan
Dick, 4564234| Hubei
Wangxiao, 4433253| Beijing
...

We need to "|" "," is the delimiter, but also filters the space, divides each row into the corresponding field. You can try it as one of your assignments and ask for code simplicity.

1.3 String Insert, replace, erase

Knowing the operators of string, finding functions and substr, you already know the operation of string 80%. Insert functions, the Replace function and the erase function are relatively simple to use. Here is an example to illustrate its application. The string simply provides the Replace function by position and interval, and cannot replace another string in the specified string with one string string. Here is a function to implement this function:

void String_replace (String & strbig, const string & strsrc, const string &strdst)
{
    String::size_ Type pos=0;
    String::size_type srclen=strsrc.size ();
    String::size_type dstlen=strdst.size ();
    while ((Pos=strbig.find (STRSRC, POS))!= String::npos)
    {
        strbig.replace (pos, Srclen, STRDST);
        POS + + Dstlen;
    }
}

See how to call:

#include <string>
#include <iostream>
using namespace std;
int main ()
{
    string strinfo= ' This is Winter, Winter is a programmer. Do you know Winter? ";
    cout<< "orign string is: \ n" <<strinfo<<endl;
    String_replace (Strinfo, "Winter", "Wende");
    cout<< "after replace Winter with Wende, the string is: \ n" <<strinfo<<endl;
    return 0;
}

Its output results:

Orign string is:
the is Winter, Winter is a programmer. Do you know Winter?
After replace Winter with Wende, the string are: This
is Wende, Wende is a programmer. Do you know Wende?

If you do not use the Replace function, you can replace it with erase and inserts, and you can implement the functions of the String_replace function:

void String_replace (String & strbig, const string & strsrc, const string &strdst)
{
    String::size_ Type pos=0;
    String::size_type srclen=strsrc.size ();
    String::size_type dstlen=strdst.size ();
    while ((Pos=strbig.find (STRSRC, POS))!= String::npos)
    {
        strbig.erase (pos, srclen);
        Strbig.insert (POS, STRDST);
        POS + + Dstlen;
    }
}

Of course, this method does not use replace directly.

2 string and C-style strings

Now that we have seen so many examples, we find that the const char* can be directly converted to string, for example, in the above example, we use the

String_replace (Strinfo, "Winter", "Wende"); to substitute void String_replace (String & strbig, const string & strsrc, const s Tring &STRDST)

With only char and const char in the C language, string provides three functions to meet its requirements for ease of use:

Const chart* C_STR () const
Const chart* data () const
size_type copy (chart* buf, Size_type n, size_type pos = 0) Co Nst

Where: C_str directly returns a string ending with a. Data returns the contents of a string directly as an array, with a value of size () returned, and no 0 characters at the end. Copy copies the contents of string into the buf space. You might ask, the C_STR () feature contains data (), and that requires the data () function. Look at the Source:

Const chart* C_STR () const
{
    if (length () = 0) return
        "";
    Terminate ();
    return data ();
}

The original C_STR () process is: Call Terminate () first, and then return data (). So if you have high efficiency requirements and your processing does not necessarily end in the same way, you'd better choose data (). But for a normal C function, you need to use the const char* as an input parameter, and you'll want to work with the C_STR () function.

For the C_STR () data () function, the returned array is owned by the string itself, and you must not modify its contents. The reason for this is that many string implementations use a reference mechanism, that is, it is possible to have several strings using the same character storage space. And you can't use sizeof (string) to see its size. Detailed explanation and implementation view effective STL clause 15: Beware of the diversity of string implementations.

In addition, in your program, only use C_STR () or data () to get the string only when needed, every time you call it, the next time you use it will fail, such as:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.