c++

What's the fastest way to generate a random sequence from a list of data?

Let's say that I have a list of data: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} where n = 10 elements I'd like to randomly choose k elements of this set to form a sublist, say k = 5. In that case, I could end up with a sublist that looks like {9, 3, 5, 2, 7} I could accomplish this by: Randomly determining an offset within the list, between 0 a...

Binding specific threads to specific processor cores

I've word a bit with parallel processing in college and now I'm trying to get better at it. I can write code that can run in parallel and then start up threads, but after that I loose control over what the threads do. I would like to know how I can control the threads to things like for example bind a specific thread to a specific proces...

operator functions

according to this site http://www.cplusplus.com/reference/std/functional/unary_function/ this code should work #include <iostream> #include <functional> using namespace std; struct isdigit : public unary_function<char,bool>{ bool operator() (char a){ return (a>='0' && a<='9');} }; int main(){ isdigit myobject; isdig...

Strange C++ exception "definition"

A student of mine submitted some C++ code similar to the following one. The code compiles and runs, but the throw statement produces the following message: terminate called after throwing an instance of 'int' If I make the function void the compiler complains invalid use of ‘void’ on the line that contains the throw statement...

Reading in Intel Hex file for sorting C++

I need to read in an Intel Hex file which looks something like this: :0300000002F8D42F :07000300020096000000005E :07000B000200B50000000037 :030013000200D414 :03001B000200F3ED (Yes, some lines are missing and sometimes 1 line only contains 1 byte) The : is the start code First 2 bytes is the byte count Next 4 are the address in mem...

Nested template possibilities

Is the following valid?: template<typename T> class C { C1<C2<T>> someMember; }; ...

How to speed up offscreen OpenGL rendering with large textures on Win32?

I'm developing some C++ code that can do some fancy 3D transition effects between two images, for which I thought OpenGL would be the best option. I start with a DIB section and set it up for OpenGL, and I create two textures from input images. Then for each frame I draw just two OpenGL quads, with the corresponding image texture. The ...

Iterate std::list<boost::variant>

How would you check for the object type when looping std::list? class A { int x; int y; public: A() {x = 1; y = 2;} }; class B { double x; double y; public: B() {x = 1; y = 2;} }; class C { float x; float y; public: C() {x = 1; y = 2;} }; int main() { A a; B b; C c; list <boost::variant<A, B, C>> l; ...

On the initialization of std::array

Let's say you have a c++0x std::array member of a template class and you want to initialize it by means of a constructor that takes a couple of iterators: template <typename Tp, size_t N> class Test { public: template <typename Iterator> Test(Iterator first, Iterator last) { if (std::distance(first,last) > N ) ...

Fancy way to read a file in C++ : strange performance issue

The usual way to read a file in C++ is this one: std::ifstream file("file.txt", std::ios::binary | std::ios::ate); std::vector<char> data(file.tellg()); file.seekg(0, std::ios::beg); file.read(data.data(), data.size()); Reading a 1.6 MB file is almost instant. But recently, I discovered std::istream_iterator and wanted to try it in o...

Could Bjarne make mistake? (while explaining templates), or I still do not understand?

Guys, I'm doing excercises from "The C++ Programming Language 3rd ed." and on page 340 there is an example of function: template <class T, class C = Cmp<T> > // Here is a default argument // But as far as I'm concerned it's illegal to have a default argument in // a function template int compare (const String<T>& str1, const St...

Do all C++ compilers generate C code?

Probably a pretty vague and broad question, but do all C++ compilers compile code into C first before compiling them into machine code? ...

Nested templates with dependent scope

What is dependent scope and what is the meaning of typename in the context of the following error? $ make g++ -std=gnu++0x main.cpp main.cpp:18:10: error: need 'typename' before 'ptrModel<std::vector<Data> >::Type' because 'ptrModel<std::vector<Data> >' is a dependent scope make: *** [all] Error 1 /* * main.cpp */ #include <vector...

C++ Exception Throw/Catch Optimizations

It seems to me that if you have some C++ code like this: int f() { try { if( do_it() != success ) { throw do_it_failure(); } } catch( const std::exception &e ) { show_error( e.what() ); } } The C++ compiler should be able to optimize the throw and catch into almost a simple goto. However, it seems to me from m...

C++ cross platform for processes: is POCO lib good? other alternatives?

Hi All I would like to use some cross platform C++ library for starting, stopping and getting standard output for processes. I found and I would like to use C++ POCO libraries: are these good? What's the best alternatives? I use Boost and they have Boost Process, but is not part of the official release and AFAIK it won't be neither ve...

Dynamic data in c++

Hi there! I'm coding in C++ and I'm in need for a dynamic data storage, like ArrayList in C# or Java. Can anyone help me with that? I'm not sure what to use. Thanks! ...

Limit function/constructor arguments to members of a particular type.

If I have a templated class that takes another class as a parameter and which is constructed from a particular type, is there anyway to limit its range of possible construction values to those that are only members of the parameter class? So, if I have template <typename T> struct Foo { Foo(int v) : value(v) {} int val...

Basic Object Oriented inheritance question

Consider the following source code. I have two classes CBar and CFoo. CFoo inherits from CBar. The output of this source code is Bar Foo Bar I was expecting Bar Foo Foo Where did I go wrong? What I was thinking was that since the CFoo object has a Speak function that overrides the CBar speak function. When I call The Speak()...

Arrays of function pointers as members of classes

Can I have, as a private member of a class, an array of function pointers? Something like, class MyClass { public: //public stuff private: void (*specficFunctions[16]) (void); } I specifically don't want to use functors or functionoids. ...

Why am I getting an error when I try to use my structure?

I have a structure Defined in the Header file for a class i am working in, and i am trying to use the Struct in one of the methods of the class. It looks basically like this: struct example { double a; int b; ... }; in the header above my class definition, and then in the cpp file, i have: void exampleclass::test(){ ...