std

std::insert_iterator and iterator invalidation

倖福魔咒の 提交于 2019-12-11 13:55:15
问题 I tried writing a generic, in place, intersperse function. The function should intersperse a given element into a sequence of elements. #include <vector> #include <list> #include <algorithm> #include <iostream> template<typename ForwardIterator, typename InserterFunc> void intersperse(ForwardIterator begin, ForwardIterator end, InserterFunc ins, // we cannot use rvalue references here, // maybe taking by value and letting users feed in std::ref would be smarter const ForwardIterator::value

How to load XML document from `std::istream`?

﹥>﹥吖頭↗ 提交于 2019-12-11 13:40:02
问题 I'd like to load TinyXml documents from std::istream , but it doesn't contain such method: /** Load a file using the current document value. Returns true if successful. Will delete any existing document data before loading. */ bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ); /// Save a file using the current document value. Returns true if successful. bool SaveFile() const; /// Load a file using the given filename. Returns true if successful. bool LoadFile( const char *

C++ std::stack Traversal

北战南征 提交于 2019-12-11 13:18:12
问题 I am using std::stack for an project and I need to travel on it for checking same values. I checked member functions but, I could not find an appropriate one for this task. The first idea that came up is using a copy stack but, the program might waste lots of extra space in this case, and not using an user-defined stack class is important at this level of the project (Yeah, I made a design mistake... ). So, any idea? Thank you! 回答1: Avoid std::stack , it's just a useless wrapper that dumbs

std::sort and std::unique problem with a struct

被刻印的时光 ゝ 提交于 2019-12-11 13:08:21
问题 The following code: #include <vector> #include <algorithm> struct myStructDim { int nId; int dwHeight; int dwWidth; }; void main() { ::std::vector<myStructDim> m_vec_dim; ::std::sort(m_vec_dim.begin(), m_vec_dim.end()); m_vec_dim.erase( ::std::unique(m_vec_dim.begin(), m_vec_dim.end()), m_vec_dim.end() ); } will not compile with many errors, such as: error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for

How to use unordered_set with compare function?

 ̄綄美尐妖づ 提交于 2019-12-11 12:48:06
问题 I wrote my own compare function for the third template parameter of std::unorderd_set. My function is static bool HasSamePosition(const Node& a, const Node& b); in the class Node. Now I'm trying to use this function in my unordered set, std::unordered_set<Node, std::hash<Node>, bool(*)(const Node& a, const Node& b)> closedlist(&Node::HasSamePosition); but it doesn't work. The error ist, that no instance of the constructor is matching the argumentlist. What am I missing? 回答1: Well the compiler

Is it possible to access the real and imaginary parts of a complex number with the [] operator in C++

孤街浪徒 提交于 2019-12-11 12:43:47
问题 I'm using the <complex> library in C++. Is it possible to access the real and complex parts of a complex number using the [] operator? I want to use myComplexNum[0] instead of myComplexNum.real() and myComplexNum[1] instead of myComplexNum.imag() . I tried to do it using MingW 4.5.2, and I receive the error: no match found for 'operator[]' . I really need to use the [] operator for now, because otherwise I would have to change hundreds of lines of code. 回答1: Nope. You could derive from std:

How do I flush a stdlib output file on win32?

柔情痞子 提交于 2019-12-11 11:44:04
问题 I have a C++ program that is writing to a file on Windows 7. When I call f.flush() the NTFS file does not get bigger. Is there any way to force the file to get flushed? 回答1: You can look here: How do I get the file HANDLE from the fopen FILE structure? the code looks like this: FlushFileBuffers((HANDLE) _fileno(_file)); do not forget call fflush(file), before call FlusFileBuffers. For std::fstream and gnu stl, I checked it on my linux box, have no windows at home, but should work with mingw,

Wrap raw data in std container like array, with runtime size

半世苍凉 提交于 2019-12-11 11:28:22
问题 Is there any std container which would be fixed size like std::array, but the size would not be compile time, but runtime? I want to pass a part of some data I have stored in std::array to std::acculumate and similar functions. I do not want to use std::vector (working on embedded platform), therefore I am looking for something in between. Assume code like this, what I want is something to be used in place of array_part : #include <array> #include <algorithm> #include <iostream> #include

C++11 for xCode errors

点点圈 提交于 2019-12-11 10:52:24
问题 I want to use std collections, for example std::vector in my xCode 4.5 project. Following the explanation here no type named 'shared_ptr' in namespace 'std' I changed my compiler options accordingly, but now when I come to build the project I get a bunch of errors such as Type float cannot be narrowed to GLubyte (aka unsigned char) in initializer list . These errors are in a ccType.h, which is part of the Cocos2d library I'm using for my game. I'm thinking the right thing to do is not to

std::array c++11 initializer syntax error

ぃ、小莉子 提交于 2019-12-11 10:44:39
问题 the std::array im getting no match for ‘operator=’ in ‘myarr = {1, 5, 2, 3, 4}’ error when compiling this code #include <iostream> #include <array> using namespace std; int main(int argc, char const *argv[]) { array<int, 5> myarr; myarr = {1,5,2,3,4}; for(auto i : myarr) { cout << i << endl; } return 0; } but it compiles when i do it on the same line array<int, 5> myarr = {1,5,2,3,4}; how to assign values on the seprate line i need to assign values in the class constructor how can i do it ?