std

What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?

最后都变了- 提交于 2019-12-04 15:41:22
问题 What's the purpose of boost::to_string (found in boost/exception/to_string.hpp ) and how does it differ from boost::lexical_cast<std::string> and std::to_string ? 回答1: std::to_string, available since C++11, works on fundamental numeric types specifically. It also has a std::to_wstring variant. It is designed to produce the same results that sprintf would. You may choose this form to avoid dependencies on external libraries/headers. The throw-on-failure function boost::lexical_cast<std::string

How do I write binary data for 7z archive format?

ε祈祈猫儿з 提交于 2019-12-04 15:00:40
I've been pouring over the format description and source code for the 7z archive format, but I'm still having trouble writing a valid container. I assume I can create an empty container... anyway here's my start: std::ofstream ofs(archivename.c_str(), std::ios::binary|std::ios::trunc); Byte signature[6] = {'7', 'z', 0xBC, 0xAF, 0x27, 0x1C}; Byte major = 0; Byte minor = 3; ofs.write((const char*)signature, 6); ofs.write((const char*)major, 1); ofs.write((const char*)minor, 1); UInt64 offset = 0; UInt64 size = 0; UInt32 crc = 0; ofs.write((const char*)offset, 4); ofs.write((const char*)size, 8);

C++ UTF-8 actual string length

心已入冬 提交于 2019-12-04 14:17:30
Is there any native (cross platform) C++ function in any of standard libraries which returns the actual length of std::string ? Update: as we know std::string.length() returns the number of bytes not the number of characters. I already have a custom function which returns the actual one, but I'm looking for an standard one. codecvt ought to be helpful, the Standard provides implementations for UTF-8, for example codecvt_utf8<char32_t>() would be appropriate in this case. Probably something like: wstring_convert< codecvt_utf8<char32_t>, char32_t >().from_bytes(the_std_string).size() Actual

Wrong use of std::copy?

依然范特西╮ 提交于 2019-12-04 11:42:53
Here is a simple test program that illustrates the problem I faced: #include <iostream> #include <stdlib.h> #include <inttypes.h> #include <vector> using namespace std; typedef unsigned char Byte; int main( ) { uint32_t ui32 = 12; size_t sizeofUi32 = sizeof ui32; cout << "sizeofUi32: " << sizeofUi32 << endl; vector<Byte> v(10); std::copy(&ui32, &ui32 + sizeof ui32, &v[4]); uint32_t result = 0; std::copy(&v[4], &v[4] + sizeof ui32, &result); cout << "Result: " << result << " sizeofUi32: " << sizeofUi32 << endl; return 0; } output: sizeofUi32: 4 Result: 12 sizeofUi32: 17179869184 I thought this

How to track memory assign by STL library

不羁岁月 提交于 2019-12-04 11:41:16
I want to track all the memory(size allocated by std lib) allocated by all STL containers like map,list,vector etc. I just want to track STL container not regular object creation. Basically want to override new and delete of std lib. Example class demo { public: int i; std::list<int> mylist; } int main() { demo dd = new demo(); // -> Don't want to track this. Just want to track // mylist(size of my list) } I found out that std has it's own allocator option. For example list has it is allocator template < class T, class Alloc = allocator<T> > class list; What is the default allocator if I don't

Why does std::setprecision(6) stream more than six digits in fixed-width mode?

喜你入骨 提交于 2019-12-04 10:05:38
The output of the following code: #include <limits> #include <iostream> #include <iomanip> #include <limits> #include <string> #include <sstream> using namespace std; inline string lexical_cast(const float arg) { stringstream ss; ss << fixed << setprecision(numeric_limits<float>::digits10) << arg; if (!ss) throw "Conversion failed"; return ss.str(); } int main() { cout << numeric_limits<float>::digits10 << '\n'; cout << lexical_cast(32.123456789) << '\n'; } is: 6 32.123455 I expected, and wanted: 6 32.1234 because, to the best of my knowledge, that's the extent of what a float can reliably

std::unique and removing duplicates from a container of objects

折月煮酒 提交于 2019-12-04 09:10:01
I would like to know if there is an efficient way to remove objects from a container based on values of member fields of the objects. For example, I can do the following using stl::unique with a list of strings: #include<iostream> #include<list> #include<string> #include<algorithm> using namespace std; bool stringCompare(const string & l, const string & r) { return (l==r); } int main() { list<string> myStrings; myStrings.push_back("1001"); myStrings.push_back("1001"); myStrings.push_back("81"); myStrings.push_back("1001"); myStrings.push_back("81"); myStrings.sort(); myStrings.erase(unique

C++ std::queue::pop() calls destructor. What of pointer types?

依然范特西╮ 提交于 2019-12-04 08:14:32
问题 I have a std::queue that is wrapped as a templated class to make a thread-safe queue. I have two versions of this class: one that stores value types, one that stores pointer types. For the pointer type, I'm having trouble deleting the elements of the queue on destruction. The reason is that I don't know a way to remove the items from the queue safely. This reference states (vacuously, so I guess it doesn't actually STATE it) that the only way to remove elements from the queue is to call pop()

Why does iostream include time.h?

為{幸葍}努か 提交于 2019-12-04 07:36:59
Consider this code: #include <iostream> template<class C> struct time { }; int main() { } It produces (GCC 4.5): error: ‘template<class C> struct time’ redeclared as different kind of symbol /usr/include/time.h:186:15: error: previous declaration of ‘time_t time(time_t*)’ Why does iostream include time_t time(time_t*) ? Why does iostream include time_t time(time_t*) outside std namespace? (unanswered) Why, if I remove template<class C> , do I not get this error? If you run g++ -H -Wall -c testim.cc (where testim.cc is your example) you'll see that .... /usr/include/c++/4.6/bits/ios_base.h ....

c++ empty std::vector begin not equals to end

时光总嘲笑我的痴心妄想 提交于 2019-12-04 07:17:10
问题 Hi I have situation on windows 10, that declared empty class member variable vector, but this vector's begin()(first iterator) and end()(last iterator) differ, as I know in empty vector those 2 should be same. Any ideas? :))) struct B { std::string a; std::string b; }; class A { A(); std::vector<B> vec_; }; A::A() { } Here in costructor A vec_.begin().base() not equals vec_.end().base() Which as I know should be equal 回答1: The standard only requires that the iterators are equal, not that