简体   繁体   中英

c++ do not understand the syntax of string::iterator

I am hoping someone can help me understand this.

I ran into this code:

vector<string> split_string(string input_string) {
    string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
        return x == y and x == ' ';
    });

I am not sure what the :: represents, I have never seen it used this way before. Maybe it is an alternative to using string.iterator ?

Also, I am a bit confused regarding to member types in a class on cppreference.com:

会员类型

string is a type of class, how do all these member types relate to string , or any other class's relationship with its own inherit member types?

I would really love some guru advice here.

In this instance, iterator is a class defined inside of string. This segment of code demonstrates how iterator would be defined relative to string

#include <iostream>
class Outer{
  public:
  class inner{
    public:
    int num;
  };
};
int main() {
  Outer::inner i;
  i.num = 0;
  std::cout << i.num;
}

The goal behind the nested classes is encapsulation , where everything the string does and its related nested classes are kept separate and/or private from everything else in the name space.

As NathanOliver pointed out, you really need a C++ book to go into depth with all of these members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM