std

error: there are no arguments to 'at' that depend on a template parameter, so a declaration of at must be available

微笑、不失礼 提交于 2019-12-04 06:55:51
Noob here, I'm trying to compile this segment of code from Bjarne Stroustrup's 'The C++ Programming Language' but CodeBlocks keeps throwing me this error. The code is about range checking an array held in a vector function. Here is the code: #include <iostream> #include <vector> #include <array> using namespace std; int i = 1000; template<class T> class Vec : public vector<T> { public: Vec() : vector<T>() { } T& operator[] (int i) {return at(i); } const T& operator[] (int i) const {return at(i); } //The at() operation is a vector subscript operation //that throws an exception of type out_of

how are map iterators invalidated when erasing elements? [duplicate]

笑着哭i 提交于 2019-12-04 06:40:36
This question already has an answer here: Iterator invalidation rules 5 answers when and how are iterators invalidated in a map when using the erase method ? for example : std :: map < int , int > aMap ; aMap [ 33 ] = 1 ; aMap [ 42 ] = 10000 ; aMap [ 69 ] = 100 ; aMap [ 666 ] = -1 ; std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ; for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ; it != itEnd ; // no-op ) { aMap.erase ( it ++ ) ; } the erased iterator will surely become invalid (it's incremented while still valid) but what about the others? if I'm not wrong

An std container inside a template method

蓝咒 提交于 2019-12-04 05:58:25
Greetings. I don't know very well how to explain myself, but I believe a piece of code will make you understand what I'm intenting to do : template<class A, class B> void myFunction(A<B>& list) { typename A<B>::iterator current = list.begin(); typename A<B>::iterator end = list.end(); while (current != end) { current++; } } Where A is an STL container (vector, list...). It's like inception, but with templates : a template, inside a template, etc... The thing is : what do you do when one of the params of your template is itself a template... and still want to support every types supported by

Convert vector<std::string> to vector<double>

时间秒杀一切 提交于 2019-12-04 04:50:21
I have a string vector like {"1.2","3.4","0.5","200.7"} . I would like to convert each element into double and store it in a vector<double> . Like so {1.2,3.4,0.5,200.7} What would be the best way to do this? I know of the std::stod(string, size) ; But I am hoping for a better way to do this. I was looking for something like: vector<double> doubleVector = convertStringVectortoDoubleVector(myStringVector); There doesn't seem to be anything like that; so what is the next best thing? EDIT: Here's what I ended up using: std::vector<double> convertStringVectortoDoubleVector(const std::vector<std:

c++ vector source code

会有一股神秘感。 提交于 2019-12-04 03:34:22
I am trying to get the vector source code to see how the standard std or stl vector is implemented. This is for learning purpose. Now the question is where can i find the source code. Even source code of other C++ Container also helpful. Steve Townsend There is no 'standard' vector - the standard defines behaviour and interface (and some implementation details, such as contiguous storage) but the code is a matter for compiler writers to determine. Your compiler should have its own <vector> header file, have you checked for this on your build include path? Once you find that you should also see

Why does stdlib.h's abs() family of functions return a signed value?

被刻印的时光 ゝ 提交于 2019-12-04 02:29:33
The negative implication of this is noted in the man page: NOTES Trying to take the absolute value of the most negative integer is not defined. What's the reasoning behind this and what's the best recourse for a person who would like to avoid undefined behavior? Do I have to resort to something like: unsigned uabs(signed val) { return val > 0 ? val : (val == 1U << ((sizeof(val) * 8) - 1)) ? -1U : -val; } (Intentionally hacky to emphasize displeasure with stdlib ;-) Example Say you had a 4-bit signed value (for ease of understanding). unsigned max is 15, signed (positive) max is 7, signed

boost::filesystem::path and fopen()

我的未来我决定 提交于 2019-12-04 01:26:10
I get error when I try to do this: path p = "somepath"; FILE* file = fopen(p.c_str(), "r"); I get: argument of type "const boost::filesystem::path::value_type *" is incompatible with parameter of type "const char *" Could anyone tell me what I'm doing wrong? Thanks If you're under Windows, that value_type is wchar_t , and will fail in the conversion for fopen (that needs a char* ). As per the documentation, it seems you have to use the string() method to obtain a standard string with a default code conversor ( wchar_t -> char ): FILE* file = fopen(p.string().c_str(), "r"); 来源: https:/

How to convert std::vector<unsigned char> to vector<char> without copying?

两盒软妹~` 提交于 2019-12-03 23:21:00
I weren't able to find that question, and it's an actual problem I'm facing. I have a file loading utility that returns std::vector<unsigned char> containing whole file contents. However, the processing function requires contiguos array of char (and that cannot be changed - it's a library function). Since the class that's using the processing function stores a copy of the data anyway, I want to store it as vector<char> . Here's the code that might be a bit more illustrative. std::vector<unsigned char> LoadFile (std::string const& path); class Processor { std::vector<char> cache; void

C++: “vector<int>::size_type variable” - what is the point of declaring in this way?

白昼怎懂夜的黑 提交于 2019-12-03 23:14:34
I think this is a very basic question but I couldn't just figure it out. I was used to using arrays in C++ but I'm now starting to learn vectors. I was making a test code, and I came across a question. First of all, here's the code I made: #include <iostream> #include <vector> #include <numeric> using namespace std; int main(){ vector<double> score(10); for(vector<double>::size_type i=0;i<20;i++) { cout<<"Enter marks for student #"<<i+1<<":"<<flush; cin>>score[i]; } double total = accumulate(score.begin(), score.end(),0); cout<<"Total score:"<<total<<endl<<"Average score:"<<total/score.size()<

std::thread <unresolved overloaded function type> error

匆匆过客 提交于 2019-12-03 22:25:02
I am trying to spawn a thread from within my class and the thread executes a particular method in my class. The code looks like this: class ThreadClass{ int myThread(int arg){ // do something } void createThread(){ thread t = thread(myThread,10); } } ; This code on compilation throws an error saying std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ThreadClass::*)(int), _Args = {int}] no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (ThreadClass::*&&)(int)’ I am not sure what is the actual bug here. Can someone help me with this? Thanks