stl

Best STL transform - like template function for ternary operators

可紊 提交于 2020-01-13 03:20:10
问题 STL defines two flavors of the transform function The first is For unary operators: template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); And the second is for binary operators: template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2

How to add std::swap for my template class? [duplicate]

三世轮回 提交于 2020-01-13 03:03:27
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: how to provide a swap function for my class? There are some questions about this, but a lot of contradictions (person A giving solution A' with many upvotes with person B saying it's UB) or "only works if the compiler supports ADL" is answered. So, say I have the following template (container) class: template<typename T> class C { // ... void swap(C<T>& y) throw(); // C x; x.swap(y); } then what is the correct

How to add std::swap for my template class? [duplicate]

那年仲夏 提交于 2020-01-13 03:03:13
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: how to provide a swap function for my class? There are some questions about this, but a lot of contradictions (person A giving solution A' with many upvotes with person B saying it's UB) or "only works if the compiler supports ADL" is answered. So, say I have the following template (container) class: template<typename T> class C { // ... void swap(C<T>& y) throw(); // C x; x.swap(y); } then what is the correct

stable_partition on forward iterators

狂风中的少年 提交于 2020-01-12 08:09:39
问题 1. In the current standard draft the specification of std::stable_partition is: template<class BidirectionalIterator, class Predicate> BidirectionalIterator stable_partition( BidirectionalIterator first, BidirectionalIterator last, Predicate pred); I didn't find the requirement that BidirectionalIterator should be a bidirectional iterator, but the name suggests so. (See below) 2. In the SGI STL, the specification is: template <class ForwardIterator, class Predicate> ForwardIterator stable

STL之deque

孤街醉人 提交于 2020-01-12 07:12:16
deque 和 vector 一样都是标准模板库中的内容, deque 是双端队列,在接口上和 vector 非常相似,在许多操作的地方可以直接替换。假如读者已经能够有效地使用vector 容器,下面提供 deque 的成员函数和操作,进行对比参考。 Deque 成员函数 函数 描述 c.assign(beg,end) c.assign(n,elem) 将 [beg; end) 区间中的数据赋值给 c 。 将 n 个 elem 的拷贝赋值给 c 。 c.at(idx) 传回索引 idx 所指的数据,如果 idx 越界,抛出out_of_range 。 c.back() 传回最后一个数据,不检查这个数据是否存在。 c.begin() 传回迭代器重的可一个数据。 c.clear() 移除容器中所有数据。 deque c deque c1(c2) Deque c(n) Deque c(n, elem) Deque c(beg,end) c.~deque() 创建一个空的 deque 。 复制一个 deque 。 创建一个 deque ,含有 n 个数据,数据均已缺省构造产生 。 创建一个含有 n 个 elem 拷贝的 deque 。 创建一个以 [beg;end) 区间的 deque 。 销毁所有数据,释放内存。 c.empty() 判断容器是否为空。 c.end()

Sorting a set<string> on the basis of length

帅比萌擦擦* 提交于 2020-01-12 05:36:08
问题 My question is related to this. I wanted to perform a sort() operation over the set with the help of a lambda expression as a predicate. My code is #include <set> #include <string> #include <iostream> #include <algorithm> int main() { using namespace std; string s = "abc"; set<string> results; do { for (int n = 1; n <= s.size(); ++n) { results.insert(s.substr(0, n)); } } while (next_permutation(s.begin(), s.end())); sort (results.begin(),results.end());[](string a, string b)->bool{ size_t

Is there a list of STL container methods that may throw an exception anywhere?

那年仲夏 提交于 2020-01-12 04:40:21
问题 I know the STL will throw on a memory allocation error or if the contained type throws in its constructor / assignment operator. Otherwise, apparently 'a few' STL methods can throw other exceptions. The example everyone seems to mention is vector::at(), but I can't find a list of the others anywhere. Does anyone know of such a list? 回答1: Won't be 100% accurate, and is for C++03 , but a half-hour effort based on grepping through GCC 4.3.4 includes, ignoring tr1 and ext but including iostream.

customize cout

感情迁移 提交于 2020-01-12 03:23:05
问题 How can I derive a class from cout so that, for example, writing to it new_cout << "message"; would be equivalent to cout << __FUNCTION__ << "message" << "end of message" << endl; 回答1: class Log { public: Log(const std::string &funcName) { std::cout << funcName << ": "; } template <class T> Log &operator<<(const T &v) { std::cout << v; return *this; } ~Log() { std::cout << " [end of message]" << std::endl; } }; #define MAGIC_LOG Log(__FUNCTION__) Hence: MAGIC_LOG << "here's a message"; MAGIC

Writing stl compatible iterators

旧城冷巷雨未停 提交于 2020-01-11 19:57:21
问题 I'm trying to convert an iterator class I have to be stl compatible so that it can be used with the stl algorithms. In the following simple (and frankly useless) example, which should print the values 0 to 5 inclusive, I am getting the following errors, ISO C++ forbids incrementing a pointer of type ‘Iterator (*)()‘ and, invalid conversion from ‘Iterator (*)()’ to ‘int‘ What am I doing wrong? Thanks. #include <iterator> #include <algorithm> #include <iostream> class Iterator : public std:

Writing stl compatible iterators

微笑、不失礼 提交于 2020-01-11 19:55:14
问题 I'm trying to convert an iterator class I have to be stl compatible so that it can be used with the stl algorithms. In the following simple (and frankly useless) example, which should print the values 0 to 5 inclusive, I am getting the following errors, ISO C++ forbids incrementing a pointer of type ‘Iterator (*)()‘ and, invalid conversion from ‘Iterator (*)()’ to ‘int‘ What am I doing wrong? Thanks. #include <iterator> #include <algorithm> #include <iostream> class Iterator : public std: