std

Is specialization of std::to_string for custom types allowed by the C++ standard?

坚强是说给别人听的谎言 提交于 2019-12-18 13:53:10
问题 In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? namespace std { string to_string(::MyClass const & c) { return c.toString(); } } Sample use-case: int main() { MyClass c; std::cout << std::to_string(c) << std::endl; } 回答1: In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? No. First of all, it is not a template function so you can't specialize it at all. If you're asking about adding your

Why is it so slow iterating over a big std::list?

送分小仙女□ 提交于 2019-12-18 12:44:25
问题 As title suggests, I had problems with a program of mine where I used a std::list as a stack and also to iterate over all elements of the list. The program was taking way too long when the lists became very big. Does anyone have a good explanation for this? Is it some stack/cache behavior? (Solved the problem by changing the lists to std::vector and std::deque (an amazing data structure by the way) and everything suddenly went so much faster) EDIT: I'm not a fool and I don't access elements

Can placement-new and vector::data() be used to replace elements in a vector?

我与影子孤独终老i 提交于 2019-12-18 12:15:32
问题 There are two existing questions about replacing vector elements that are not assignable: C++ Use Unassignable Objects in Vector How to push_back without operator=() for const members? A typical reason for an object to be non-assignable is that its class definition includes const members and therefore has its operator= deleted. std::vector requires that its element type be assignable. And indeed, at least using GCC, neither direct assignment ( vec[i] = x; ), nor a combination of erase() and

How does std::endl not use any brackets if it is a function?

筅森魡賤 提交于 2019-12-18 11:45:25
问题 The question is pretty much in the title. According to C++ Reference, std::endl is actually a function. Looking at its declaration in <iostream> , this can be verified. However, when you use std::endl , you don't use std::endl() . Instead, you use: std::cout << "Foo" << std::endl; In fact, if you use std::endl() , the compiler demands more parameters, as noted on the link above. Would someone care to explain this? What is so special about std::endl ? Can we implement functions that do not

Why is using vector of pointers considered bad?

一个人想着一个人 提交于 2019-12-18 11:35:41
问题 Recently I've met with opinion that I shouldn't use vector of pointers. I wanted to know - why I cant? For example if I have a class foo it is possible to do this: vector <foo*> v; v.push_back(new foo()); I've already seen some people down voting such practices, why is that? 回答1: Storing plain pointers in a container can lead to memory leaks and dangling pointers. Storing a pointer in a container does not define any kind of ownership of the pointer. Thus the container does not know the

Why is using vector of pointers considered bad?

删除回忆录丶 提交于 2019-12-18 11:34:11
问题 Recently I've met with opinion that I shouldn't use vector of pointers. I wanted to know - why I cant? For example if I have a class foo it is possible to do this: vector <foo*> v; v.push_back(new foo()); I've already seen some people down voting such practices, why is that? 回答1: Storing plain pointers in a container can lead to memory leaks and dangling pointers. Storing a pointer in a container does not define any kind of ownership of the pointer. Thus the container does not know the

How to inspect std::string in GDB with no source code?

天大地大妈咪最大 提交于 2019-12-18 10:47:07
问题 I'm trying to debug a program that has no source code available, and I need to look at what it has stored in a std::string. I've been Googling and looking on here, and I've found some information about outputting STL containers, but all of it refers to variables, with no source or debug information all I have is a memory offset of the class data. Is there any way to do this? 回答1: Every std::string implementation has a pointer to the raw characters in it somewhere. For g++ 4.x , that pointer

Ambiguous overload call to abs(double)

独自空忆成欢 提交于 2019-12-18 10:15:11
问题 I have the following C++ code: #include <math.h> #include <cmath.h> // per http://www.cplusplus.com/reference/clibrary/cmath/abs/ // snip ... if ( (loan_balance < 0) && (abs(loan_balance) > loan_payment) ) { ... } and make blows up on: error: call of overloaded 'abs(double)' is ambiguous also of interest: /usr/include/stdlib.h:785: note: candidates are: int abs(int) How can I specify that the compiler needs to call the abs() in cmath.h that can handle floats? Compiler info (Not sure if this

Error when i do not include string header file in c++

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:40:54
问题 #include<iostream> #include<string> using namespace std; void main(){ string str="abc"; cout<<str; system("pause"); } If i do not include string header file then there is an error at << in line cout< I thought the error will be at line where str is defined. 回答1: Standard library headers can include other standard library headers, even if not specified in the standard. So it may be that with your implementation, the iostream header includes some parts of the string header, so that std::string

seekg() function fails

本小妞迷上赌 提交于 2019-12-18 09:06:35
问题 I am trying to write some simple code which will read a text file but reads the first line twice. I thought this would be as simple as something like this std::ifstream file; file.open("filename", std::ios_base::in); std::string line; std::getline(file, line); // process line file.seekg(0, ios::beg); while (std::getline(file, line)) { // process line } However the seekg must fail as the first line is not processed twice. Any idea why? PLEASE NOTE: This is not the problem I am faced with but a