stl

How can I create a std::set of structures?

家住魔仙堡 提交于 2020-02-13 11:55:33
问题 I need to create a stl::set of structures. Therefore, I wrote the following: stl::set <Point> mySet; // Point - name of the structure. Then I tried to add a structure instance to mySet as follows: Point myPoint; mySet.insert(myPoint); However, I get several compilation errors (error C2784, error C2676): 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &): failed to bring

STL学习(二)vector

徘徊边缘 提交于 2020-02-11 06:43:29
一.介绍 是动态数组,是连续的空间,如果空间不够用,会申请一个更大的连续的空间,同时 迭代器失效 头文件为#include< vector >; 二.构造函数 (为了方便省略< T >) vector(); 无参数的构造 vector(count); n个元素,和数组类似 vector( n,elem ); 用num个val来初始化容器 5个1 6个‘a’ vector( const vector &from ); 拷贝构造 一定是同种类型元素的vector 用另一个vector对象初始化当前的vector对象 vector( begin,end ); 构造函数将[a,b)区间元素拷贝给本身,注意该区间 左开右闭 三.属性 (一)容量 Visual Studio定义对象的时候初始化几个,无参数就是零 容量不够时增加现有容量的一半。比如现有10个,新的就是15个,现有13个;增加13/2==6个,就是19个 VC++6.0 无参数就是0 容量不够时增加现有容量的一倍。比如现有10个,新的就是20个;现有20个,新的就是40个 reserve(); //可以提前预留空间以提高效率, vector < int > v ( 10 ) ; //capacity为10,size也为10,里面放有元素,加数据要重新申请内存 vector < int > v ; v . reserve ( 10

Constructing std::function argument from lambda

一世执手 提交于 2020-02-10 15:15:18
问题 I have the following templated function (C++ latest standard is enabled in the compiler - but maybe 17 would be enough). #include <functional> template<typename TReturn, typename ...TArgs> void MyFunction(const std::function<TReturn(TArgs...)>& callback); int main() { MyFunction(std::function([](int){})); MyFunction([](int){}); } The first call compiles, when I explicitly convert it to std::function, but the second case does not. In the first case the template deduction is done automatically,

高效使用STL

限于喜欢 提交于 2020-02-09 11:34:25
1、 当对象很大时,建立指针的容器而不是对象的容器 注意事项:   1)容器 销毁前需要自行销毁指针所指向的对象; 否则就造成了内存泄漏;   2)使用 排序等算法时 , 需要构造 基于对象的 比较函数 ,如果使用默认的比较函数,其结果是基于指针大小的比较,而不是对象的比较; 二、用 empty() 代替 size()来检查是否为空 因为对于 list, size()会遍历每一个元素来确定大小,时间复杂度 o( n),线性时间; 而 empty总是保证常数时间; 三、尽量用区间成员函数代替单元素操作 使用区间成员函数有以下好处: 1)更少的函数调用; 2)更少的元素移动; 3)更少的内存分配 例:将 v2后半部的元素赋值给 v1: 单元式操作: for (vector::const_iterator ci = v2.begin() + v2.size() / 2; ci != v2.end(); ++ci)   v1.push_back(*ci) 使用区间成员函数 assign(): v1.assign(v2.begin() + v2.size() / 2, v2.end()); 四、使用 reserver避免不必要的内存分配 (for vector) 如果预先知道空间的大小,预先分配了空间 避免了重新分配空间和复制的代价 ; 注: reserve()只是修改了容量,并非大小,向

how portable is end iterator decrement?

自闭症网瘾萝莉.ら 提交于 2020-02-09 05:08:27
问题 Just encountered decrement of end() iterator in my company source codes and it looks strange for me. As far as I remember this was working on some platforms, but not for the others. Maybe I'm wrong, however I couldn't find anything useful in standard about that. Standard only says that end() returns an iterator which is the past-the-end value, but is it guaranteed to be decrementable? How does code like that match the standard? std::list<int>::iterator it = --l.end(); Thanks in advance. 回答1:

STL学习(一)string

倾然丶 夕夏残阳落幕 提交于 2020-02-09 03:25:57
介绍 string是专门的字符串操作的一个类,非常强大。字符串CString,QString。string 是一个类, 这个类将以上的内容封装到一起,使得字符串的操作更灵活,方式更多,管理更合理。string这个类使用的时候不用考虑内存的分配与释放,也不用担心越界崩溃,因为前辈在封装string的时候,已经把几乎所有情况都考虑到并处理了。 头文件< string >,注意要using namespace std; 构造函数 string str; string str ; str = "abcd" ; cout << str << endl ; string( size_type length, char ch ); string str ( 4 , 'a' ) ; cout << str << endl ; string( const char *str ); string str ( "abcd" ) ; cout << str << endl ; string( const char *str, size_type length ); string str ( "abcde" , 2 ) ; //前两个即ab cout << str << endl ; string( string &str, size_type index, size_type length );

Standard Template Library (STL) - std::map::size

China☆狼群 提交于 2020-02-09 03:07:34
Standard Template Library (STL) - std::map::size public member function - 公开成员函数 1. std::map::size C++98 size_type size() const; C++11 size_type size() const noexcept; Return container size - 返回容纳的元素数 Returns the number of elements in the map container. 返回 map 容器中的元素数。 2. Parameters none - 无 3. Return value The number of elements in the container. 容器中的元素数量。 Member type size_type is an unsigned integral type. 4. Examples 4.1 std::map::size //============================================================================ // Name : std::map::size // Author : Yongqiang Cheng // Version : Version 1.0

c++ push_back doesn't work as it is supposed

☆樱花仙子☆ 提交于 2020-02-08 09:45:51
问题 I have a class symbol_table that has a vector of objects of another class row_st.also I have an enter method where inserts objects of row_st with a passed name into the vector of desired symbol_table.but when I call the enter to enter objects with name : a;b;c;Iwill get the following result: a,b,c;b,c;c.the first element of vector gets the name of all the entered objects. and the second element also gets the name of the later entries. class row_st { public: char* name; type_u type;//int:0

字符串替换 string (stl) find + replace

倾然丶 夕夏残阳落幕 提交于 2020-02-08 04:40:59
题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=113 字符串替换 时间限制: 3000 ms | 内存限制: 65535 KB 难度: 2 描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 输入包含多行数据 每行数据是一个字符串,长度不超过1000 数据以EOF结束 输出 对于输入的每一行,输出替换后的字符串 样例输入 you are what you do 样例输出 we are what we do分析:1:find(string&s, location) , 从 location 位置开始,在字符串中找子串s, 找到返回找到的第一个首字符位置,否则返回 string::npos。2:replace(location, size, string &s) , 从location 位置开始, 数 size 个 数的 字符串, 用 字符串s 代替。3: cin>>string s , 遇到空白结束输入 。 如果想输入一行, 则用 getline(cin, S).代码如下: #include <cstdlib> #include <cstring> #include <algorithm> #include <cstdio> #include <cmath> #include <iostream>

C++ - STL - queue

一个人想着一个人 提交于 2020-02-08 01:50:05
C++ - STL - queue 整理笔记 一 基本概念:    queue是一种 先进先出 的数据结构,有两个出口,一端新增元素,另一端移除元素; 队列中只有队头和队尾才可以被外界使用,因此队列不 允许有遍历行为 。   c++队列模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为 deque 类型。 二 头文件    #include<queue> 三 定义一个 queue类队列    queue<类型> 名字; 四 相关函数 ( 1)push() 在末尾加入一个元素 #include <iostream> #include <queue> using namespace std; int main() { queue<int> q; q.push(1); q.push(2); cout<<q.front()<<endl; return 0; }   输出:1 ( 2)front()返回队列第一个元素的引用。注意:只是返回第一个元素的值,并没有删除! #include <iostream> #include <queue> using namespace std; int main() { queue<int> q; q.push(1); q.push(2); cout<<q