c++-standard-library

How can I use a std::valarray to store/manipulate a contiguous 2D array?

本秂侑毒 提交于 2019-11-27 12:56:18
How can I use a std::valarray to store/manipulate a 2D array? I'd like to see an example of a 2D array with elements accessed by row/column indices. Something like this pseudo code: matrix(i,j) = 42; An example of how to initialize such an array would also be nice. I'm already aware of Boost.MultiArray, Boost.uBlas, and Blitz++. Feel free to answer why I shouldn't use valarray for my use case. However, I want the memory for the multidimensional array to be a contiguous (columns x rows) block. No Java-style nested arrays. Off the top of my head: template <class element_type> class matrix {

Are the experimental features of modern C++ reliable for long-term projects?

坚强是说给别人听的谎言 提交于 2019-11-27 11:24:20
问题 I have a project that currently uses C++11/14, but it requires something like std::filesystem , which is only available in C++17, and hence I don't have a chance to currently use it. I see, however, that it's available in my current compiler as std::experimental::filesystem . Is it a good idea to use experimental features, assuming that I could in the future add something like: #ifdef CXX17 //if this is C++17 std::filesystem::something ...; #else std::experimental::filesystem::something ...;

How to correctly use std::reference_wrappers

荒凉一梦 提交于 2019-11-27 10:48:41
问题 I am trying to understand std::reference_wrapper . The following code shows that the reference wrapper does not behave exactly like a reference. #include <iostream> #include <vector> #include <functional> int main() { std::vector<int> numbers = {1, 3, 0, -8, 5, 3, 1}; auto referenceWrapper = std::ref(numbers); std::vector<int>& reference = numbers; std::cout << reference[3] << std::endl; std::cout << referenceWrapper.get()[3] << std::endl; // I need to use get ^ // otherwise does not compile.

Why isn't there int128_t?

橙三吉。 提交于 2019-11-27 09:00:26
A number of compilers provide 128-bit integer types, but none of the ones I've used provide the typedefs int128_t . Why? As far as I recall, the standard Reserves int128_t for this purpose Encourages implementations that provide such a type to provide the typedef Mandates that such implementations provide an intmax_t of at least 128 bits (and, I do not believe I've used an implementation that actually conforms to that last point) I'll refer to the C standard; I think the C++ standard inherits the rules for <stdint.h> / <cstdint> from C. I know that gcc implements 128-bit signed and unsigned

std::string.resize() and std::string.length()

你。 提交于 2019-11-27 08:19:03
问题 I'm relatively new to C++ and I'm still getting to grips with the C++ Standard Library. To help transition from C, I want to format a std::string using printf-style formatters. I realise stringstream is a more type-safe approach, but I find myself finding printf-style much easier to read and deal with (at least, for the time being). This is my function: using namespace std; string formatStdString(const string &format, ...) { va_list va; string output; size_t needed; size_t used; va_start(va,

std::istream operator exception reset / not thrown

喜欢而已 提交于 2019-11-27 08:05:52
问题 I'm not sure about how to use std::istream::exception according to the standard , to let std::istream::operator>> throw an exception if it can't read the input into a variable, e.g. double. The following code has different behavior with clang/libc++ and gcc/libstdc++: #include <iostream> #include <cassert> int main () { double foo,bar; std::istream& is = std::cin; is.exceptions(std::istream::failbit); is >> foo; //throws exception as expected with gcc/libstdc++ with input "ASD" std::cout <<

Why was std::pow(double, int) removed from C++11?

旧时模样 提交于 2019-11-27 06:52:01
While looking into Efficient way to compute p^q (exponentiation), where q is an integer and reviewing the C++98 and C++11 standards I noticed that apparently the std::pow(double, int) overload was removed in C++11. In C++98 26.5/6 it has the double pow(double, int); signature. In C++11 26.8 all I could find was overloads taking a pair of float , double , or long double , and an explicit note that in case of a mixture of parameter types integral&double, that the pow(double, double) overload should be picked. Is this just a clarification of the previous intention, were they incorrectly added in

std::lexical_cast - is there such a thing?

Deadly 提交于 2019-11-27 06:47:00
Does the C++ Standard Library define this function, or do I have to resort to Boost? I searched the web and couldn't find anything except Boost, but I thought I'd better ask here. Only partially. C++11 <string> has std::to_string for the built-in types: [n3290: 21.5/7]: string to_string(int val); string to_string(unsigned val); string to_string(long val); string to_string(unsigned long val); string to_string(long long val); string to_string(unsigned long long val); string to_string(float val); string to_string(double val); string to_string(long double val); Returns: Each function returns a

Why is there no transform_if in the C++ standard library?

妖精的绣舞 提交于 2019-11-27 03:52:38
A use case emerged when wanting to do a contitional copy (1. doable with copy_if ) but from a container of values to a container of pointers to those values (2. doable with transform ). With the available tools I can't do it in less than two steps : #include <vector> #include <algorithm> using namespace std; struct ha { int i; explicit ha(int a) : i(a) {} }; int main() { vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector // GOAL : make a vector of pointers to elements with i < 2 vector<ha*> ph; // target vector vector<ha*> pv; // temporary vector // 1. transform(v.begin(), v.end(), back

Android ndk std::to_string support

試著忘記壹切 提交于 2019-11-27 03:42:49
I'm using android NDK r9d and toolchain 4.8 but I'm not able to use std::to_string function, compiler throws this error: error: 'to_string' is not a member of 'std' Is this function not supported on android ndk? I try APP_CPPFLAGS := -std=c++11 with no luck. yushulx You can try LOCAL_CFLAGS := -std=c++11 , but note that not all C++11 APIs are available with the NDK's gnustl . Full C++14 support is available with libc++ ( APP_STL := c++_shared ). The alternative is to implement it yourself. #include <string> #include <sstream> template <typename T> std::string to_string(T value) { std: