stl

STL iterator for MFC container CObList

给你一囗甜甜゛ 提交于 2020-01-14 03:19:10
问题 I have a Folder class which contains two lists of Folders and Files . class Folder : public CObject { public: typedef std::string StringT; ... Container filesInFolder; Container foldersInFolder; StringT folderName; ... }; class File : public CObject { ... }; Lists derive from CObList. Class Container : public CObList . I need to perform search inside foldersInFolder by name and in filesInFolder by name and extension. CObList::Find does not allow me to use predicates. I would normaly use std:

STL set 使用小结

对着背影说爱祢 提交于 2020-01-14 02:48:35
这是微软帮助文档中对集合(set)的解释: “描述了一个控制变长元素序列的对象(注:set中的key和value是Key类型的,而map中的key和value是一个pair结构中的两个分 量)的模板类, 每一个元素包含了一个排序键(sort key)和一个值(value)。对这个序列可以进行查找、插入、删除序列中的任意一个元素,而完成这些操作的时间同这个序列中元素个数的对数成比例关 系, 并且当游标指向一个已删除的元素时,删除操作无效。” 而一个经过更正的和更加实际的定义应该是:一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。这在收集一个数据的具体值的时候是有用的。集 合中的元素按一定的顺序排列,并被作为集合中的实例。如果你需要一个键/值对(pair)来存储数据,map是一个更好的选择。一个集合通过一个链表来组 织,在插入操作和删除操作上比向量(vector)快,但查找或添加末尾的元素时会有些慢。 #include<iostream> #include<string> #include<set> using namespace std; int main() { set<string> strset; set<string>::iterator iter; strset.insert("apple"); strset.insert("orange"); strset

2_STL容器

安稳与你 提交于 2020-01-14 02:47:21
STL算法的精髓在于 算法的 返回值!!! String:   string是STL的字符串类型,通常用来表示字符串;C语言中一般用char*   char*字符指针;string是类封装了char*,管理这个字符串,是一个char*型的容器;   string不用考虑内存释放和越界,string管理char*所分配的内存。   string提供了一系列字符串操作函数(find,copy,erase,replace,insert) 初始化 :   string s1 = "abcd"; //重载operator=   string s2("abcd") //有参构造函数   string s3 = s2; //初始化调用的是拷贝构造函数,跟赋值调用operator=()不同   string s1(3,'a'); //s1的内容:aaa;将字符复制n份 遍历: 通过数组的方式: for(int i = 0; i < s1.length(); i++) { cout << s1[i] << " "; } 通过迭代器: for(string::iterator k = s1.begin(); k = s1.end(); k++) { cout << *k << " "; } 通过at()函数: 与数组的[]方法相比,at()可以抛出异常,提示下标越界;而[]不抛出异常直接中断程序。

std::map with std::pair keys where pair elements has no order importance

≡放荡痞女 提交于 2020-01-14 01:44:18
问题 As the question says, I need to use std::map in such way that. std::map<std::pair<int, int>, int*> m; int* a_ptr = new int; *a_ptr = 15; m[std::make_pair(1, 2)] = a_ptr; std::cout << *m[std::make_pair(2, 1)] << std::endl; //should output 15 Now, in my actual implementation all the keys and values are actually pointers. How should I approach this problem? Two ideas come to my mind. One is I should write a function that every time I am using m[] to access or to write into map, I should also m

Cannot access vector when constructing with istream_iterator range

主宰稳场 提交于 2020-01-13 20:28:11
问题 I tried to compile this code snippet but I got compiler error :( ! Compile with Visual Studio 2010 #include <vector> #include <string> #include <sstream> #include <iterator> #include <iostream> using namespace std; int main() { string s( "Well well on" ); istringstream in( s ); vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() ); copy( v.begin(), v.end(), ostream_iterator<string>( cout, "\n" ) ); } Errors: Error 1 error C2228: left of '.begin' must have class/struct

Cannot access vector when constructing with istream_iterator range

依然范特西╮ 提交于 2020-01-13 20:28:06
问题 I tried to compile this code snippet but I got compiler error :( ! Compile with Visual Studio 2010 #include <vector> #include <string> #include <sstream> #include <iterator> #include <iostream> using namespace std; int main() { string s( "Well well on" ); istringstream in( s ); vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() ); copy( v.begin(), v.end(), ostream_iterator<string>( cout, "\n" ) ); } Errors: Error 1 error C2228: left of '.begin' must have class/struct

【STL】旧键盘打字

僤鯓⒐⒋嵵緔 提交于 2020-01-13 18:40:31
描述 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样? 输入 输入在2行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过105个字符的串。可用的字符包括字母[a-z, A-Z]、数字0-9、以及下划线“_”(代表空格)、“,”、“.”、“-”、“+”(代表上档键)。题目保证第2行输入的文字串非空。 注意:如果上档键坏掉了,那么大写的英文字母无法被打出。 输出 在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。 样例输入 7+IE. 7_This_is_a_test. 样例输出 _hs_s_a_tst 题目来源 PAT 乙级 分析:map用一下,easy的。 代码: #include<bits/stdc++.h> using namespace std; int main(){ string a,b; getline(cin,a); getline(cin,b); map<char,int>m; int la=a.length(),lb=b.length(); for (int i=0;i<la;i++) { m[a[i]]=1; if (a[i]>=‘A’&&a[i]<=‘Z’) { m[a[i]-‘A’+‘a’]=1; }

标准C++中string类及STL容器类简介

久未见 提交于 2020-01-13 12:09:13
一.标准C++库字符串类std::string的用法 #include std::string s1; std::string s3(s2); std::string s2("this is a string"); begin 得到指向字符串开头的Iterator end 得到指向字符串结尾的Iterator rbegin 得到指向反向字符串开头的Iterator rend 得到指向反向字符串结尾的Iterator size 得到字符串的大小 length() 和size函数功能相同 max_size 字符串可能的最大大小 capacity 在不重新分配内存的情况下,字符串可能的大小 empty 判断是否为空 operator[] 取第几个元素,相当于数组 c_str 取得C风格的const char* 字符串 data 取得字符串内容地址 operator= 赋值操作符 reserve 预留空间 swap 交换函数 insert 插入字符 append 追加字符 push_back 追加字符 erase 删除字符串 clear 清空字符容器中所有内容 resize 重新分配空间 assign 和赋值操作符一样 replace 替代 copy 字符串到空间 find 查找,返回基于0的索引号 rfind 反向查找 find_first_of 查找包含子串中的任何字符,返回第一个位置

std::multiset and finding the middle element

不羁的心 提交于 2020-01-13 10:44:09
问题 I have std::multiset which If i iterate over from std::multiset::begin() to std::multiset::end() i will get sorted elements. How Do I get the middle element in this std::multiset other than iterating from std::multiset::begin() to std::multiset::begin() + size() / 2 回答1: Here is a solution to get median value for std::multiset: template<class T> double GetMedian(const std::multiset<T>& data) { if (data.empty()) throw std::length_error("Cannot calculate median value for empty dataset"); const

Bitset Reference

做~自己de王妃 提交于 2020-01-13 10:34:08
问题 From http://www.cplusplus.com/reference/stl/bitset/: Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements. How, exactly, does this bit reference work? The only way I could think of would be to use a static array of char s, but then each instance would need to store its index in the array. Since each reference instance would have at least the size of a size_t , that would destroy the