stl

Seg fault after is item pushed onto STL container

房东的猫 提交于 2019-12-21 19:56:07
问题 typedef struct temp { int a,b; char *c; temp(){ c = (char*)malloc(10);}; ~temp(){free(c);}; }temp; int main() { temp a; list<temp> l1; l1.push_back(a); l1.clear(); return 0; } giving segmentation fault. 回答1: You don't have a copy constructor. When you push 'a' into the list, it gets copied. Because you don't have a copy constructor (to allocate memory for c and copy from old c to new c) c is the same pointer in a and the copy of a in the list. The destructor for both a's gets called, the

column vector with row means — with std::accumulate?

泪湿孤枕 提交于 2019-12-21 17:22:21
问题 In an effort to be as lazy as possible I read in a matrix as vector< vector<double> > data ( rows, vector<double> ( columns ) ); and try to use as many STL goodies as I can. One thing I need to do next is to compute the row means. In C-style programming that would be vector<double> rowmeans( data.size() ); for ( int i=0; i<data.size(); i++ ) for ( int j=0; j<data[i].size(); j++ ) rowmeans[i] += data[i][j]/data[i].size(); In In C++, how to compute the mean of a vector of integers using a

How to read entire stream into a std::vector?

对着背影说爱祢 提交于 2019-12-21 17:09:51
问题 I read an answer here showing how to read an entire stream into a std::string with the following one (two) liner: std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream), eos); For doing something similar to read a binary stream into a std::vector , why can't I simply replace char with uint8_t and std::string with std::vector ? auto stream = std::ifstream(path, std::ios::in | std::ios::binary); auto eos = std::istreambuf_iterator<uint8_t>(); auto buffer = std:

How can I use std::binary_search using just a key?

不想你离开。 提交于 2019-12-21 17:07:05
问题 I have some data that is stored in a sorted vector. This vector is sorted by some key. I know the STL has an algorithm for checking if an element is in this sorted list. This means I can write something like this: struct MyData { int key; OtherData data; }; struct MyComparator { bool operator()( const MyData & d1, const MyData & d2 ) const { return d1.key < d2.key; } }; bool isKeyInVector( int key, const std::vector<MyData> &v ) { MyData thingToSearchFor; thingToSearchFor.key = key; return

What are the functions in the standard library that can be implemented faster with programming hacks? [closed]

北城以北 提交于 2019-12-21 16:59:36
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I have recently read an article about fast sqrt calculation. Therefore, I have decided to ask SO community and its experts to help me find out, which STL algorithms or mathematical calculations can be implemented faster with programming hacks? It would be great if you can give

Dump hex float in C++

て烟熏妆下的殇ゞ 提交于 2019-12-21 15:19:15
问题 I tried following: std::cout << std::hex << 17.0625; But it dumped it in decimal. I'd like to see 11.01 (17.0625 in hex). How can I print some floating point value in hex? Please do not offer solutions like: void outhexdigits(std::ostream& out, fp_t d, int max_chars=160) { while(d > 0. && max_chars) { while(d < 1. && max_chars){ out << '0'; --max_chars; d*=16; } if (d>=1. && max_chars) { int i = 0; while (d>=1.) ++i, --d; out << std::hex << i; --max_chars; } } } Is there any way to dump float

Merits of std::find

北战南征 提交于 2019-12-21 12:37:09
问题 Is there any advantage to using C++11's std::find over a container's find method? In the case of std::vector (which does not have a find method) does std::find use some smart algorithm or the naive way of simply iterating over every element? In the case of std::map it seems you need to pass along an std::pair , which is the value_type of an std::map . This does not seem very useful as usually you'd want to find for either a key or a mapped element. What about other containers like std::list

How to create a structure which contains a list of itself?

情到浓时终转凉″ 提交于 2019-12-21 12:33:24
问题 I want to create a structure which contains a list of same structure like this: #include <list> struct Url { CString strUrl; std::list<Url> children; }; int main() { Url u1, u2; u1.children.push_back(u2); } This code is not compiling. But when I replace std::list with std::vector it is working fine. How can I make this working with std::list ? Output window contains the following error. c:\program files\microsoft visual studio\vc98\include\list(29) : error C2079: '_Value' uses undefined

STL之list容器用法

落爺英雄遲暮 提交于 2019-12-21 12:26:36
List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。 使用list容器之前必须加上<vector>头文件:#include<list>; list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std; 构造函数   list<int> c0; //空链表   list<int> c1(3); //建一个含三个默认值是0的元素的链表   list<int> c2(5,2); //建一个含五个元素的链表,值都是2   list<int> c4(c2); //建一个c2的copy链表   list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。 成员函数 c.begin() 返回指向链表第一个元素的迭代器。 c.end() 返回指向链表最后一个元素之后的迭代器。 1 list<int> a1{1,2,3,4,5}; 2 list<int>::iterator it; 3 for(it = a1.begin();it!=a1.end();it++){ 4 cout << *it << "

How to std::hash an unordered std::pair

巧了我就是萌 提交于 2019-12-21 11:55:50
问题 I want to be able to use a std::pair as a key in an unordered_container. I know that I could do this the following way: template<typename T> void hash_combine(std::size_t &seed, T const &key) { std::hash<T> hasher; seed ^= hasher(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename T1, typename T2> struct hash<std::pair<T1, T2>> { std::size_t operator()(std::pair<T1, T2> const &p) const { std::size_t seed(0); ::hash_combine(seed, p.first); ::hash_combine(seed, p