关于C++中用两个迭代器方式初始化string的知识

string(iter1, iter2);

第一点:两个迭代器必须指向同一个容器。
第二点:iter2必须>=iter1。
第三点:假设iter1等于iter2,那么结果为空[]

另外一个比較特殊的关于反向迭代器的很实用知识点,用例如以下程序来说明:

int main() {
    string str1 = "abc";
    cout << "str1.rend() - str1.rbegin() is " << str1.rend() - str1.rbegin() << endl;
    cout << "str1.rbegin() - str1.rend() is " << str1.rbegin() - str1.rend() << endl;
    cout << "*str1.rbegin() is " << *str1.rbegin() << endl;
    cout << string(str1.rbegin(), str1.rend()) << endl;
    //cout << *str1.rend() << endl; error
    //cout << string(str1.rend(), str1.rbegin()) << endl; error
    getchar();
}

output is

str1.rend() - str1.rbegin() is 3
str1.rbegin() - str1.rend() is -3
*str1.rbegin() is c
cba
原文地址:https://www.cnblogs.com/gcczhongduan/p/5130841.html