stl

Arithmetic on end() iterator

[亡魂溺海] 提交于 2021-01-26 09:15:35
问题 Let A be a std::vector<double> , Is this well-defined? if(!A.empty()) std::vector<double>::iterator myBack = A.end() - 1; Is the end iterator only good for equalities and inequalities checks? Or I can perform some pointer arithmetic as long as I remain in the container? On my platform this code works. I'm wondering if this is portable. 回答1: It is perfectly valid as vector::iterator is a random access iterator. You can perform arithmetic operations on it and it is not platform dependent. std:

Arithmetic on end() iterator

此生再无相见时 提交于 2021-01-26 09:11:50
问题 Let A be a std::vector<double> , Is this well-defined? if(!A.empty()) std::vector<double>::iterator myBack = A.end() - 1; Is the end iterator only good for equalities and inequalities checks? Or I can perform some pointer arithmetic as long as I remain in the container? On my platform this code works. I'm wondering if this is portable. 回答1: It is perfectly valid as vector::iterator is a random access iterator. You can perform arithmetic operations on it and it is not platform dependent. std:

std::sort() on a vector of Class pointers

╄→尐↘猪︶ㄣ 提交于 2021-01-26 07:13:34
问题 I have a vector of class pointers std::vector<Square*> listSquares . I want to sort it with one of the attributes of the class as the key. This is what I'm doing bool compById(Square* a, Square* b) { return a->getId() < b->getId(); } std::sort(listSquares.begin(), listSquares.end(), compById) but the compiler says: error: no matching function for call to 'sort(std::vector::iterator, std::vector::iterator, <unresolved overloaded function type>)' what am I doing wrong here? 回答1: In order to use

std::sort() on a vector of Class pointers

时光总嘲笑我的痴心妄想 提交于 2021-01-26 07:12:33
问题 I have a vector of class pointers std::vector<Square*> listSquares . I want to sort it with one of the attributes of the class as the key. This is what I'm doing bool compById(Square* a, Square* b) { return a->getId() < b->getId(); } std::sort(listSquares.begin(), listSquares.end(), compById) but the compiler says: error: no matching function for call to 'sort(std::vector::iterator, std::vector::iterator, <unresolved overloaded function type>)' what am I doing wrong here? 回答1: In order to use

std::sort() on a vector of Class pointers

本秂侑毒 提交于 2021-01-26 07:12:32
问题 I have a vector of class pointers std::vector<Square*> listSquares . I want to sort it with one of the attributes of the class as the key. This is what I'm doing bool compById(Square* a, Square* b) { return a->getId() < b->getId(); } std::sort(listSquares.begin(), listSquares.end(), compById) but the compiler says: error: no matching function for call to 'sort(std::vector::iterator, std::vector::iterator, <unresolved overloaded function type>)' what am I doing wrong here? 回答1: In order to use

How does std::sort work for list of pairs?

不问归期 提交于 2021-01-22 03:57:01
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

折月煮酒 提交于 2021-01-22 03:56:52
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

僤鯓⒐⒋嵵緔 提交于 2021-01-22 03:53:57
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

How does std::sort work for list of pairs?

泄露秘密 提交于 2021-01-22 03:52:46
问题 Why does this: #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<pair<int, string>> list; int main() { int one = 1, two = 2, three =3, five =5, six = 6; string bla = "bla"; list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(one, bla)); list.push_back( pair<int, string>(two, bla)); list.push_back( pair<int, string>(six, bla)); list.push_back( pair<int, string>(five, bla)); sort(list.begin(), list.end()); for

Why does std::queue use std::dequeue as underlying default container?

不打扰是莪最后的温柔 提交于 2021-01-21 07:32:08
问题 As read on cplusplus.com, std::queue is implemented as follows: queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front". The underlying container may be one of the standard container class template or some other specifically designed