- 博客(74)
- 资源 (1)
- 收藏
- 关注
原创 11.4
11.37 http://www.cs.fsu.edu/~lacher/courses/COP4531/fall13/lectures/containers2/slide04.html11.38 @pezy #include <unordered_map>#include <set>#include <string>#include <iostream>#include <fstre
2017-01-31 15:13:24 277
原创 11.3.6
11.33#include<iostream>#include<fstream>#include<string>#include<sstream>#include<map>using std::ifstream;using std::getline;using std::string;using std::istringstream;using std::cout;using s
2017-01-31 14:28:17 257
原创 11.3.5
11.27 当我想知道知道,对应关键字的元素有几个的时候,我会使用count 当我只想知道它在不在的时候,我会使用find11.28std::map<std::string ,std::vector<int>> hh{ {"p",{0,1,2,3}} }auto iter= hh.find("p");11.29 lower_bound和upper_bound 会返回一样的迭代器,指向关键
2017-01-30 21:36:16 222
原创 11.3.4
11.24map<int, int> m;m[0] = 1;map中加入一个pair{ 0 , 1}11.25 vector<int> v; v[0] = 1;试图给v[0] 赋值,但是v[0] 根本不存在,所以行为未定义11.26std::map<int, std::string> m = { {0,"a" },{ 1,"b" } };可以用int来进行下标操作 下标操作返回一个strin
2017-01-30 20:37:29 281
原创 11.3.2
11.20#include <iostream>#include <map>#include <string>using std::string;using std::map;using std::cin;using std::cout;int main(){ map<string, size_t> counts; for(string word; cin >> word
2017-01-30 20:01:21 189
原创 11.3.1
11.15mapped_type : vector< int >key_type : intvalue_type : std::pair< int, vector<int> >11.16std::map<int, std::string> map;map[10010] = "132";std::map<int, std::string>::iterator it = map.begin();
2017-01-30 18:57:35 292
原创 11.2.3
11.12 std::vector<std::pair<std::string, int>> vec; while (std::cin >> str >> i) vec.push_back(std::pair<std::string, int>(str, i));11.13vec.push_back(std::pair<std::string, int>(str, i));v
2017-01-30 17:10:54 225
原创 11.2.2
11.9std::map<std::string,std::list<size_t>> m;11.10std::map<std::vector<int>::iterator, int> mv;std::map<std::list<int>::iterator, int> ml;vector可以 ,定义了比较的方法 而list不行,没有定义比较的方法11.11#include <set>auto
2017-01-30 14:23:24 229
原创 Randomized quickSort
C/C++中产生随机数(rand,srand用法) http://blog.csdn.net/PYPARA/article/details/54773968 Quick sort http://blog.csdn.net/pypara/article/details/54773861C++:int partition(int *a, int lhs, int rhs);int
2017-01-29 21:38:16 523
转载 C/C++中产生随机数(rand,srand用法)
计算机的随机数都是由伪随机数,即是由小M多项式序列生成的,其中产生每个小序列都有一个初始值,即随机种子。(注意: 小M多项式序列的周期是65535,即每次利用一个随机种子生成的随机数的周期是65535,当你取得65535个随机数后它们又重复出现了。)我们知道rand()函数可以用来产生随机数,但是这不是真正意义上的随机数,是一个伪随机数,是根据一个数(我们可以称它为种子)为基准以某个递推公式推算出来
2017-01-29 21:16:47 415
原创 Quick sort
PARTITION(A, p, q)⊳A[p. . q] x←A[p] ⊳pivot= A[p] i←p forj←p+ 1 toq do ifA[j] ≤x theni←i+ 1 exchange A[i] ↔A[j] exchange A[p] ↔A[i] return i
2017-01-29 20:36:25 296
原创 Fibonacci number
Recursive definition:Fn=0 if n= 0; =1 if n= 1; =Fn–1+ Fn–2 if n≥2. Naive recursive algorithm:Ω(φn) (exponential time), where φ=is the golden ratio Bottom-up: •Compute
2017-01-29 15:52:18 356
原创 Powering a number
Powering a numberProblem:Compute an, where n ∈N.Naive algorithm:Θ(n).an=an/2 ⋅an/2 if n is even; =a(n–1)/2 ⋅a(n–1)/2 ⋅a if n is odd. Divide-and-conquer algorithm:T(n) = T(n/2
2017-01-29 15:26:55 367
原创 11.2.1
11.5 set中保存一个key值。同时它也value值, map不仅有一个key值,还有一个与它相关联的value值,如果你需要另一个 ,你就需要使用map,如果不需要就使用set11.6 @http://stackoverflow.com/questions/2302681/c-stl-list-vs-setListSearching (linear time).Inserting, d
2017-01-29 15:04:49 214
原创 11.1
11.1 map是一种关联容器 vector是一种顺序容器 顺序容器强调值的排序,而关联式容器则在值中选择一个值作为一个关键字,这个关键字起到对值的索引的作用,方便查找。11.2 list适合需要频繁插入删除的容器 vector适合一般情况 deque适合在两端的操作 map适合关键字-值的操作 set则是关键字就是值11.3Map count() { Map counts;
2017-01-29 14:04:16 280
原创 BinarySearch
/*Find an element in a sorted array:1.Divide:Check middle element.2.Conquer:Recursively search 1subarray.3.Combine:Trivial.*/C++:bool BinarySearch(int* a,int lhs,int rhs,int val) { if (lhs > r
2017-01-29 00:00:28 241
原创 10.6
10.42#include <iostream>#include <string>#include <list>using std::string; using std::list;void elimDups(list<string> &words){ words.sort(); words.unique();}int main(){ list<string> l
2017-01-28 17:05:29 228
原创 10.5.3
10.41replace(beg, end, old_val, new_val);//在迭代器范围内,把指定旧值替换成新值replace_if(beg, end, pred, new_val); // 当pred是true时,把值替换成新值replace_copy(beg, end, dest, old_val, new_val); // 把迭代器范围内的值拷贝给dest,传入dest中旧值替换
2017-01-28 17:04:56 341
原创 10.5.1
10.38Input iterators : ==, !=, ++, *, ->Output iterators : ++, *Forward iterators : ==, !=, ++, *, ->Bidirectional iterators : ==, !=, ++, --, *, ->Random-access iterators : ==, !=, <, <=, >, >=, +
2017-01-28 17:00:13 313
原创 10.4.3
10.34 for (auto rit = vec.crbegin(); rit != vec.crend(); ++rit) std::cout << *rit << " "; std::cout << std::endl;10.35 for (auto it = std::prev(vec.cend()); true; --it) { std::cou
2017-01-28 16:43:35 203
原创 10.4.2
10.29#include<iostream>#include<iterator>#include<string>#include<fstream>#include<vector>using std::cin;using std::cout;using std::istream;using std::string;int main() { std::ifstream ifs
2017-01-28 16:38:05 302
原创 10.4.1
10.26back_inserter 使用 push_back.front_inserter 使用 push_front.insert 接受第二个参数,这个参数必须指向给定容器的迭代器10.27std::unique_copy(vec.begin(), vec.end(), back_inserter(lst));10.28 std::vector<int> vec{ 1, 2, 3, 4, 5
2017-01-28 14:33:23 188
原创 10.3.4
10.22bool func(const string &s, string::size_type sz){ return s.size() <= sz;}std::cout << count_if(v.cbegin(), v.cend(), bind(func, _1, 6));10.23 bind接受n+1个参数,n是arg_list 中参数的数目10.24auto check_
2017-01-27 21:35:30 206
原创 10.3.3
10.20std::size_t bigerThan6(vector<string> const& v){ return count_if(v.cbegin(), v.cend(), [](string const& s){ return s.size() > 6; });}10.21 int i = 5; auto check_and_decrement =
2017-01-27 19:59:48 259
原创 10.3.2
10.14auto sum = [](int a,int b){ return a + b }10.15auto sum2 = [int c](int a){ return a + c }10.16#include <iostream>#include <string>#include <vector>#include <algorithm>void println(const std::v
2017-01-27 15:00:10 232
原创 10.3.1
10.11#include <iostream>#include <string>#include <vector>#include <algorithm>void println(const std::vector<std::string> & vs) { for (auto a : vs) { std::cout << a << " "; } st
2017-01-27 13:37:22 272
原创 10.2.3
10.9#include <iostream>#include <string>#include <vector>#include <algorithm>void println(const std::vector<std::string> & vs) { for (auto a : vs) { std::cout << a << " "; } std
2017-01-27 13:16:39 221
原创 10.2.2
10.6vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; fill_n(vec.begin(), vec.size(), 0);10.7vector<int> vec;list<int> lst;int i;while (cin >> i) lst.push_back(i);vec.resize(lst.size());copy
2017-01-27 10:31:25 238
原创 10.2.1
10.3std::vector<int> v = { 0,1, 2, 3, 4 };std::cout << std::accumulate(v.cbegin(), v.cend(), 0) << std::endl;10.4std::vector<double> vd = { 1.1, 0.5, 3.3 };std::cout << std::accumulate(vd.cbegin(),
2017-01-27 09:52:15 230
原创 10.1
10.1#include<algorithm>#include<iostream>#include<vector>int main() { std::vector<int> vi; int buffer = 0; while (std::cin >> buffer) { vi.push_back(buffer); } std::cout
2017-01-26 21:30:46 257
原创 Merge sort
pseudocode:MERGE-SORTA[1 . . n]1.If n= 1, done.2.Recursively sort A[ 1 . . ⎡n/2⎤]and A[ ⎡n/2⎤+1 . . n ] .3.“Merge”the 2sorted lists.C++: const int N = 100;void merge(int * a,int lhs,int mid,int rhs
2017-01-26 14:39:24 225
原创 9.6
9.52 @pezy#include <stack>#include <string>#include <iostream>using std::string; using std::cout; using std::endl; using std::stack;int main(){ string expression{ "This is (pezy)." }; bool
2017-01-25 21:06:04 370
原创 9.5.5
9.50 @Yue Wang#include <iostream>#include <string>#include <vector>auto sum_for_int(std::vector<std::string> const& v){ int sum = 0; for (auto const& s : v) sum += std::stoi(s);
2017-01-25 21:05:24 270
原创 9.5.3
@pezy 9.47#include <string>#include <iostream>using std::string;using std::cout;using std::endl;int main(){ string numbers{ "123456789" }; string alphabet{ "abcdefghijklmnopqrstuvwxyzABCDE
2017-01-25 21:03:41 363
原创 9.5.2
9.43void replace_with(string &s, string const& oldVal, string const& newVal){ for (auto cur = s.begin(); cur <= s.end() - oldVal.size(); ) if (oldVal == string{ cur, cur + oldVal.size() })
2017-01-25 21:01:46 276
原创 9.5.1
9.41 vector<char> v{ 'p', 'y'}; string str(v.cbegin(), v.cend());9.42reserve(150); //分配足够多的空间
2017-01-25 20:58:01 303
原创 9.4
9.35 capacity是在不分配更多空间的情况下,能容纳最大元素的数目 size是目前已经容纳元素的数目9.36 不可能9.37 list不是连续的 array是固定大小的9.38#include <iostream>#include <string>#include <vector>int main(){ std::vector<std::string> v; s
2017-01-25 20:22:41 205
原创 9.3.6
9.31 @pezy#include <iostream>#include <list>using std::list;using std::cout;using std::advance;auto remove_evens_and_double_odds(list<int>& data){ for(auto cur = data.begin(); cur != data.end(
2017-01-25 20:00:38 227
原创 9.3.5
9.29vec.resize(100); // 添加75个0在末尾vec.resize(10); // 删除末尾90个元素9.30 如果要增加元素,元素必须有默认值或者默认的构造函数。
2017-01-25 19:40:01 275
原创 9.3.4
9.27#include<forward_list>#include<iostream>void removeodds(std::forward_list<int> &fl) { auto prev = fl.before_begin(); auto curr = fl.begin(); while (curr != fl.end()) { if (*c
2017-01-25 19:33:18 210
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人