stl

《STL源码剖析》——第七、八章:仿函数与接配器

南楼画角 提交于 2019-12-25 01:03:37
第七章:仿函数 7.1 、仿函数(函数对象)概观 STL仿函数的分类,若以操作数(operand)的个数划分,可分为一元和二元仿函数,若以功能划分,可分为算术运算 ( Arithmetic)、关系运算 ( Rational)、逻辑运算 ( Logical)三大类。任何应用程序欲使用STL内建的仿函数,都必须含人<functiona1>头文件,SGI则将它们实际定义于<st1_function.h>文件中。以下分别描述。 重载 () 所以函数的对象 使用()像函数调用 是类 而不是普通的函数 内部记录状态: 作为类型 与模板进行配合使用 1、函数对象通常不定义构造函数和析构函数,所以在 构造和析构时不会发生 任何问题,避免了函数调用的运行时问题。 2、函数对象超出普通函数的概念,函数对象可以有自己的状态 3、函数对象 可内联编译,性能好 。用 函数指针几乎不可能 4、模版函数对象使函数对象具有通用性,这也是它的优势之一 7.2 、可配接( adaptable )的关键 unary_function unary_function用来呈现一元函数的参数型别和回返值型别。其定义非常简单: binary_function binary_function 用来呈现二元函数的第一参数型别、第二参数型别,以及回返值型别。其定义非常简单: 7.3 、算术类( Arithmetic )仿函数

vector of structs inline initialization. braced-init-list

爱⌒轻易说出口 提交于 2019-12-25 00:23:20
问题 I ran into a strange issue which unfortunately, I cannot reproduce outside my application itself. The situation is as below. I have the following struct. struct Foo { std::string first; std::string second; bool flag; float value; }; One of my classes has a member std::vector<Foo> fooList which undergoes the following set of operations periodically. void someOperation() { UpdateFooList(); //< Updates existing members of fooList UseFooList(); //< Do some useful stuff with the new values.

Which sorting algorithm is used in stl and .net base library default search?

微笑、不失礼 提交于 2019-12-24 20:27:25
问题 I am now working on an imprived version of merge sort. I implemented it with C++ and C#. Then compared them with the stl sort and array.sort() algorithm respectively. In C++ I have got an equal (sometimes better) result. But in C#, I had to use unsafe code for using pointers. Here the performence is not that much comparable with default sort. So, I want to know- 1. Which algorithms are used in stl and .net base class library?(Better with links) 2. Do unsafe codes has performence issues? 3.

Can I extend std::map::lower_bound to search on non-key_type arguments?

情到浓时终转凉″ 提交于 2019-12-24 19:29:57
问题 Here is an illustration of my situation. I have a std::map and I want to find the first pair<key,value> where the key is any member of an equivalence class of keys. #include <map> struct Category { int foo; int bar; bool operator < (const Category & rhs) const; bool operator > (const Category & rhs) const; }; struct Key { Category category; float quality; bool operator < (const Key & rhs) const { if (category < rhs.category) return true; else if (category > rhs.category) return false; else

problems using STL std::transform from cygwin g++

会有一股神秘感。 提交于 2019-12-24 19:27:09
问题 I am running g++(gcc version 3.4.4) on cygwin. I can't get this small snippet of code to compile. I included the appropriate headers. int main(){ std::string temp("asgfsgfafgwwffw"); std::transform(temp.begin(), temp.end(), temp.begin(), std::toupper); std::cout << "result:" << temp << std::endl; return 0; } I have not had any issues using STL containers such as vector. Does anyone have any suggestions or insights into this situation. Thanks. 回答1: This explains it quite well. Which will boil

C++ case declaration? [closed]

不问归期 提交于 2019-12-24 19:23:38
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago . I am trying to adapt a digital electronics problem to a C++ STL based program. Originally I have 4 inputs C1, C2, C3, C4. This means I have a total of 16 combinations: 0000 0001 . . . 1111 I have a multimap

Using custom allocator for boost::wregex

 ̄綄美尐妖づ 提交于 2019-12-24 19:09:16
问题 I created a custom allocator and want to use it all across our code. The way we did it is by defining the templates that wrap each containers we use and make use of our own allocator instead of the default: template <class Type> class myVector : public std::vector<Type, CCustomAllocator<Type>> and so we can use it in our code like this: myVector<int> x . This reduces the chance of making mistakes. we have similar wrappers for all the containers we use in our code: list , string , wstring , ..

C++ iterator question

放肆的年华 提交于 2019-12-24 18:13:43
问题 I saw an interview question, which was asked to use "iterator" to read vector<vector<int>> . We have to design the necessary interface? Quite confusing about does this question want to ask? Or how to answer this kind of question. I can imagine that it intends to test C++ STL implementation and objected-oriented design. 回答1: Matrix is in 3*4 dimension. If needed to access only through iterators, this should give you an idea - vector< vector<int> > Matrix(3, vector<int>(3,4)); for( vector

std::set deleter in C++ is not working

会有一股神秘感。 提交于 2019-12-24 17:50:02
问题 Here I have defined my own deleter , but not sure why isn't working . Second is there any way of not using for_each loop and just defining deleter using it at the time of set declaration makes all objects get deleted automatically when the set object goes out of scope . #include <iostream> #include <memory> #include <string> #include <set> #include <algorithm> using namespace std; class A { int i; public: A(int pi):i(pi) {} int intake() const { return i; } void show() { cout<<i<<endl; } ~A()

Add a vector to a vector [duplicate]

一个人想着一个人 提交于 2019-12-24 17:48:07
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C++: Appending a vector to a vector Can I easily sum a vector to another vector? What I mean is, push_back a vector to another vector: {1, 2, 3} + {4, 8} = {1, 2, 3, 4, 8}; Do I have to do this manually: for (int i = 0; i < to_sum_vector.size(); i++) { first_vector.push_back(to_sum_vector.at(i)); } Or is there a C++/STL way of doing it? Thank you! 回答1: You can. The STL way is using insert : first_vector.insert