stl

What is the role of **std::setprecision()** without **std::fixed** in c++?

北城余情 提交于 2020-01-22 15:20:06
问题 As shown in the tutorial http://www.cplusplus.com/reference/iomanip/setprecision/ // setprecision example #include <iostream> // std::cout, std::fixed #include <iomanip> // std::setprecision int main () { double f =3.14159; std::cout << std::setprecision(5) << f << '\n'; // prints 3.1416 and not 3.141459 why std::cout << std::setprecision(9) << f << '\n'; std::cout << std::fixed; std::cout << std::setprecision(5) << f << '\n'; std::cout << std::setprecision(9) << f << '\n'; return 0; } The

Do Standard Library (STL) Containers support a form of nothrow allocation?

佐手、 提交于 2020-01-22 13:31:45
问题 The new operator (or for PODs, malloc/calloc) support a simple and efficient form of failing when allocating large chunks of memory. Say we have this: const size_t sz = GetPotentiallyLargeBufferSize(); // 1M - 1000M T* p = new (nothrow) T[sz]; if(!p) { return sorry_not_enough_mem_would_you_like_to_try_again; } ... Is there any such construct for the std::containers, or will I always have to handle an (expected!!) exception with std::vector and friends? Would there maybe be a way to write a

STL find函数

核能气质少年 提交于 2020-01-22 10:19:25
本博客是转载其他大佬的文章,觉得他写的很好所以转载。侵权删除 1、 find()函数的调用形式为 find(start,end,value) start搜寻的起点,end搜寻的终点,要寻找的value值   容器的表示方法:find(a.begin(),a.end(),value) 数组的表示方法:find(a,a+length,value) 所有的返回,均是迭代器(容器)或指针(数组),而非是直观感觉上的索引下标。如果在查找范围内不存在,返回a.end(),这里需要注意的是,a.end()不在查找范围内。 2. vector没有实现find函数,除此之外,常见容器都实现了自己的find函数。 这个案例是洛谷p1540 #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int m,n,t,cnt=0; cin>>m>>n; vector<int> v; while(cin>>t) { if(find(v.begin() , v.end()) , t) == v.end()) { v.push_back(t); cnt++; } if(v.size()>m) v.erase(v.begin()); } cout<<cnt; return 0; }

make_pair() in C++

回眸只為那壹抹淺笑 提交于 2020-01-22 03:48:29
问题 I was doing the problem 337 from leetcode. This is the code I implemented. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int rob(TreeNode* root) { unordered_map<TreeNode*, int> memo; return robSub(root, memo); } private: int robSub(TreeNode* root, unordered_map<TreeNode*, int>& memo) { if (root == nullptr) { return 0; } if (memo.find(root) !=

std::remove does not work

送分小仙女□ 提交于 2020-01-21 22:40:00
问题 The goal of my test program is to erase a cell in a simple vector of strings like below. The program fail (segmentation fault). static void display(std::vector<std::string> const &vec) { std::vector<std::string>::const_iterator It = vec.begin(); for (; It != vec.end(); ++It) std::cout << *It << " "; std::cout << std::endl; } int main(void) { std::vector<std::string> vec; size_t index = 0; vec.push_back("Toto"); vec.push_back("Titi"); vec.push_back("Tata"); vec.push_back("Tutu"); display(vec);

std::remove does not work

江枫思渺然 提交于 2020-01-21 22:38:04
问题 The goal of my test program is to erase a cell in a simple vector of strings like below. The program fail (segmentation fault). static void display(std::vector<std::string> const &vec) { std::vector<std::string>::const_iterator It = vec.begin(); for (; It != vec.end(); ++It) std::cout << *It << " "; std::cout << std::endl; } int main(void) { std::vector<std::string> vec; size_t index = 0; vec.push_back("Toto"); vec.push_back("Titi"); vec.push_back("Tata"); vec.push_back("Tutu"); display(vec);

STL Heap

白昼怎懂夜的黑 提交于 2020-01-21 15:49:36
文章目录 概述 常见函数 make_heap pop_heap push_heap sort_heap 测试代码 测试结果 概述 ♦STL 并没有把heap作为一种容器组件,它是实现优先队列的助手。它的实现是依靠vector表现的完全二叉树。 ♦STL中默认是最大堆,但是用户利用自定义的compare_fuction函数实现最小堆。 ♦heap是一个类属算法,在头文件#include< algorithm>中声明。 常见函数 make_heap pop_heap push_heap sort_heap 测试代码 # include <iostream> # include <vector> # include <algorithm> using namespace std ; int main ( ) { int n ; cin >> n ; vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { int num ; cin >> num ; v . push_back ( num ) ; } make_heap ( v . begin ( ) , v . end ( ) ) ; cout << "init max heap:" << v . front ( ) << endl ; pop_heap ( v . begin ( )

How can I extend stl classes to have my own methods? [duplicate]

て烟熏妆下的殇ゞ 提交于 2020-01-21 15:12:08
问题 This question already has answers here : Advice on a better way to extend C++ STL container with user-defined methods (8 answers) Closed 6 years ago . I know that it is a bad idea to inherit from stl classes. But is there any other way to extend them? Let's say that to improve readability, I wanted to be able to call an "add" method on my vectors instead of the less readable "push_back". Or perhaps I want to add a simple hasKey method to my std::map. Is there any way that I could do that,

Changing the reserve memory of C++ vector

强颜欢笑 提交于 2020-01-21 05:06:22
问题 I have a vector with 1000 "nodes" if(count + 1 > m_listItems.capacity()) m_listItems.reserve(count + 100); The problem is I also clear it out when I'm about to refill it. m_listItems.clear(); The capacity doesn't change. I've used the resize(1); but that doesn't seem to alter the capacity. So how does one change the reserve? 回答1: vector<Item>(m_listItems).swap(m_listItems); will shrink m_listItems again: http://www.gotw.ca/gotw/054.htm (Herb Sutter) If you want to clear it anyway, swap with

Comparing default-constructed iterators with operator==

拟墨画扇 提交于 2020-01-21 03:00:26
问题 Does the C++ Standard say I should be able to compare two default-constructed STL iterators for equality? Are default-constructed iterators equality-comparable? I want the following, using std::list for example: void foo(const std::list<int>::iterator iter) { if (iter == std::list<int>::iterator()) { // Something } } std::list<int>::iterator i; foo(i); What I want here is something like a NULL value for iterators, but I'm not sure if it's legal. In the STL implementation included with Visual