stl

How can I use std::copy to read directly from a file stream to a container?

a 夏天 提交于 2019-12-30 09:56:46
问题 I ran across a cool STL example that uses istream_iterators to copy from std input (cin) to a vector. vector<string> col1; copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(col)); How would I do something similar to read from a file-stream directly into a container? Let's just say its a simple file with contents: "The quick brown fox jumped over the lazy dogs." I want each word to be a separate element in the vector after the copy line. 回答1: Replace cin with file

memory management & std::allocator

十年热恋 提交于 2019-12-30 09:34:14
问题 In reviewing my code I see some "ugly" structure I use, in a class (called "map") I have a vector which contains a "data" class: std::vector<PointerToHUGEClass> vector; Where PointerToHUGEClass is just like the name describes. (though the object pointed too is also owned by the map class, and created with the "new" parameter in the constructor). This works all good (at the moment). However I still feel it is more of a work-around. The only reason I am using a "PointerToHUGEClass" instead of

Dynamically allocated arrays or std::vector

一个人想着一个人 提交于 2019-12-30 08:53:11
问题 I'm trying to optimize my C++ code. I've searched the internet on using dynamically allocated C++ arrays vs using std::vector and have generally seen a recommendation in favor of std::vector and that the difference in performance between the two is negligible. For instance here - Using arrays or std::vectors in C++, what's the performance gap?. However, I wrote some code to test the performance of iterating through an array/vector and assigning values to the elements and I generally found

An initial value assumption about map in c++

梦想与她 提交于 2019-12-30 08:30:47
问题 I am initializing a map map<string,int> ex; in C++. I could not find contains_key or similar function in stl, hence I am just using ex[inputString]++; The debugger shows the int to be initialized to zero correctly, is it a good assumption? 回答1: Yes, values that do not exist when accessed with operator[] are default-constructed. For numeric values, this is 0. That said, you're looking for the count method: bool hasElement = ex.count("element"); 来源: https://stackoverflow.com/questions/2346481

What is the definition of _Rb_tree_increment in bits/stl_tree.h?

走远了吗. 提交于 2019-12-30 08:23:05
问题 I want to learn codes of the red-black tree in stl. And I found a function named _Rb_tree_increment in the file bits/stl_tree.h it writes: 143 _GLIBCXX_PURE _Rb_tree_node_base* 144 _Rb_tree_increment(_Rb_tree_node_base* __x) throw (); But I can not find the definition of this function. Anyone can help? Thank you very much. 回答1: Like @Mike Seymour said, I found the definition on the library's source path, more precisely inside gcc-4.8.1/libstdc++-v3/src/c++98/tree.cc : static _Rb_tree_node

Access to elements of array of arrays using common iterator

允我心安 提交于 2019-12-30 08:13:28
问题 Is it undefined behaviour in C++ to access elements in adjacent arrays as in following code? #include <type_traits> #include <algorithm> #include <iterator> int main() { int a[10][10]; static_assert(std::is_standard_layout< decltype(a) >::value, "!"); std::fill(std::begin(*std::begin(a)), std::end(*std::prev(std::end(a))), 0); struct B { int b[10]; }; B b[10]; static_assert(std::is_standard_layout< decltype(b) >::value, "!"); std::fill(std::begin(std::begin(b)->b), std::end(std::prev(std::end

What are allocators and when is their use necessary? [closed]

为君一笑 提交于 2019-12-30 08:12:33
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . While reading books on C++ and the standard library, I see frequent references to allocators. For example, Nicolai Josuttis's The C++ Standard Library discusses them in detail in the last chapter, and both items 10 ("be aware of allocators' conventions & restrictions") and 11 ("understand the

Does STL sort use swap or binary copy?

你说的曾经没有我的故事 提交于 2019-12-30 08:10:13
问题 I'm having trouble finding a good answer to this. For some reason I thought STL sort would be implemented using swap for better support of complicated types, but as I ended up digging through the code a bit it appears it is actually doing a binary copy. Can someone confirm this? I guess binary copy would actually be preferred to swap. Side Question : Are any of the STL algorithms or container operations implemented using swap? (Outside of std::swap obviously.) I want to be aware of when it is

Why use one vs the other: `boost::shared_array` VS `boost::shared_ptr<std::vector>`?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 07:07:20
问题 So to deal with large blobs of memory either for an image or similar there are clearly lots of options. Since I'm a fan of smart pointers and RAII I'm wondering about whether it's smarter to go with : a shared_ptr to a std::vector or to go with a shared_array pointing to a dynamically allocated array. What are the conceptual, practical, and performance implications of choosing one vs the other? 回答1: It's the same as comparing std::vector vs. C array. Think about shared_array as a RAII C array

Shift-JIS decoding fails using wifstrem in Visual C++ 2013

可紊 提交于 2019-12-30 05:05:07
问题 I am trying to read a text file encoded in Shift-JIS (cp 932) using std::wifstream, and std::getline. The following code works in VS2010 but fails in VS2013: std::wifstream in; in.open("data932.txt"); const std::locale locale(".932"); in.imbue(locale); std::wstring line1, line2; std::getline(in, line1); std::getline(in, line2); const bool good = in.good(); The file contains several lines, where the first line contains just ASCII characters, and the second is Japanese script. Thus, when this