std

Lifetime of references in STD collections

旧城冷巷雨未停 提交于 2019-12-08 18:16:34
问题 How long is a reference to an element returned by an STD collection, such as a map, valid? For example, in this code: struct Employee{ int salary; string name; // the key }; map<string,Employee> allemployees; ... Employee & Joe = allemployees["Joe Smith"]; Joe.salary=150; // change "Joe Smith"'s salary assert(allemployees["Joe Smith"].salary==150); //always true .... allemployees["Mark Jones"]= Employee(); ... // No "Joe Smith" operations in the dots Joe.salary=200; assert (allemployees["Joe

compute mean using std::accumulate fails

点点圈 提交于 2019-12-08 17:30:31
问题 I'm trying to compute the mean value of a vector of doubles using the following code (compiled with g++ mean.cc -std=c++0x ): // mean.cc #include <algorithm> #include <iostream> #include <vector> struct Mean { unsigned int n; Mean(unsigned int n) : n(n) {} double operator()(double sum, double x) { return sum + x/n; } }; int main () { std::vector<double> v = {1,2,3,4,5,6}; Mean mean(v.size()); std::cout << "mean: " << std::accumulate(v.begin(), v.end(), 0, mean) << "\n"; return 0; } The mean

boost::regex vs std::regex - can't find empty() method?

こ雲淡風輕ζ 提交于 2019-12-08 17:22:21
问题 Replacing boost::regex with std::regex since we are using gcc 4.6 in the company I ran into an issue with empty () method of that class - it basically didn't make it from boost::regex into std::regex class. I am not sure whether this is a gcc's issue or this method didn't make it into C++11 standard at all, but that piece of code was heavily depending on this feature. So the question is - is there a way in C++11 std::regex to check if expression was ever set or I should stick to boost::regex

Initialising std::discrete_distribution in VS2013

这一生的挚爱 提交于 2019-12-08 16:29:25
问题 I have a std::vector<float> weights; containing the list of weights. I don't know what will be in this list until some stage in running the program. I would like to do std::discrete_distribution<> dist(weights.begin(),weights.end()); but VS2013 doesn't appear to have a constructor for std::discrete_distribution that accepts an iterator range. Is there any workaround? 回答1: Compare cppreference.com and the Microsoft reference for std::discrete_distribution : These are the constructors provided

Handling “Thrown exception type is not nothrow copy constructible” Warning

丶灬走出姿态 提交于 2019-12-08 15:16:47
问题 Going back to C++ development after a 12 years hiatus. I'm using JetBrains's CLion software which is great since it provides a lot of input on possible issues on my class design. One of the warning I get in my class' constructor throw statement is: Thrown exception type is not nothrow copy constructible . Here is a code sample that generates this warning: #include <exception> #include <iostream> using std::invalid_argument; using std::string; class MyClass { public: explicit MyClass(string

error: call of overloaded distance is ambiguous

不想你离开。 提交于 2019-12-08 12:37:28
问题 I have a bit of code (which I didn't write, but am trying to compile) -- iostream_combo.cc--, and doing so gives me the following error: ./moses/moses/comboreduct/combo/iostream_combo.cc: In function ‘std::__cxx11::string opencog::combo::l2ph(const string&, const std::vector<std::__cxx11::basic_string<char> >&)’: ./moses/moses/comboreduct/combo/iostream_combo.cc:543:64: error: call of overloaded ‘distance(std::vector<std::__cxx11::basic_string<char> >::const_iterator, __gnu_cxx::__normal

std::for_each, object with operator() overloaded not maintaining state

醉酒当歌 提交于 2019-12-08 10:06:57
问题 In trying to answer this question I came up with the following code: #include <string> #include <iostream> #include <algorithm> #include <vector> class Sizes { public: void operator() ( std::vector<int> v ) { sizeVec.push_back( v.size() ); } std::vector<int> sizeVec; }; void outFunc (int i) { std::cout << " " << i; } int _tmain(int argc, _TCHAR* argv[]) { std::vector<std::vector<int>> twodVec; std::vector<int> vec; vec.push_back( 6 ); twodVec.push_back( vec ); vec.push_back( 3 ); twodVec.push

std::vector size in header

风流意气都作罢 提交于 2019-12-08 07:04:30
问题 I have small question about std::vector. In main.h i try to make fixed size int vector std::vector<int> foo(7); But g++ gived this error: ../test/main.h:21:26: error: expected identifier before numeric constant std::vector<int> foo(7); ../main/main.h:21:26: error: expected ',' or '...' before numeric constant How can i create private vector variable of fixed size length? Or should i simply make in constructor for(int i=0; i<7;i++){ foo.push_back(0); } 回答1: Assuming foo is a data member, your

Segmentation fault in std function std::_Rb_tree_rebalance_for_erase ()

旧街凉风 提交于 2019-12-08 04:39:02
问题 (Note to any future readers: The error, unsurprisingly, is in my code and not std::_Rb_tree_rebalance_for_erase () ) I'm somewhat new to programming and am unsure how to deal with a segmentation fault that appears to be coming from a std function. I hope I'm doing something stupid (i.e., misusing a container), because I have no idea how to fix it. The precise error is Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x000000000000000c

operator << overload

不打扰是莪最后的温柔 提交于 2019-12-08 02:54:18
问题 //using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream& operator<< (ostream&, const Dog&); public: char* name; char* breed; char* gender; Dog(); ~Dog(); }; im trying to overload the << operator. I'm also trying to practice good coding. But my code wont compile unless i uncomment the using namespace std. i keep getting this error and i dont know. im using g++ compiler. Dog.h:20: error: ISO C++ forbids declaration of ‘ostream’ with no type