c++

Choosing N random numbers from a set

I have a sorted set (std::set to be precise) that contains elements with an assigned weight. I want to randomly choose N elements from this set, while the elements with higher weight should have a bigger probability of being chosen. Any element can be chosen multiple times. I want to do this as efficiently as possible - I want to avoid ...

Given the following typedefs, what syntax would be used to insert an object?

I inherited some C++ code and am trying to extend it and need to insert some objects into the following data structures in order to call a method, as the ObjectList is part of the parameter list that is passed to the method. typedef std::vector <std::pair <std::string, ObjectPtr> > ObjectListBase; typedef boost::shared_ptr <Object...

convert string to integer in c++

Hello I know it was asked many times but I hadn't found answer to my specific question. I want to convert only string that contains only decimal numbers: For example 256 is OK but 256a is not. Could it be done without checking the string? Thanks ...

Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

Not to be confused with how to split a string parsing wise, e.g.: http://stackoverflow.com/questions/236129/how-to-split-a-string I am a bit confused as to how to split a string onto multiple lines in c++. This sounds like a simple question, but take the following example: #include <iostream> #include <string> main() { //Gives error...

C++, Negative RGB values of pixels using OpenCV

I'm using OpenCV to iterate through an image and find the colour of each pixel, here's some of the code I'm using: IplImage* img = cvLoadImage("c:\\test.png"); int pixels = img->height * img->width; int channels = img->nChannels; for (int i = 0; i < pixels*channels; i+= channels) { unsigned char red = img->imageData[i + 2]; u...

Segfault in C++ calling virtual method on object created in pre-allocated buffer

Hmm... Title is a bit of a mouthful, but I'm really not sure which part of this is causing issues, I've run through it a ton of times, and can't pinpoint why... The idea is for a single Choice instance to be able to store any one value of any of the types passed in to it's template list... It's kind of like a union, except it keeps trac...

Get console handle

Hi, How do I get the console handle of an external application? I have a program running as a console. I have a 2nd program that will call GetConsoleScreenBufferInfo, but for that I need the console handle of the first program. Is it possible that given the HWND of the 1st program I can get its console handle? ...

Calling Haskell from C++ code

I'm currently writing an app in C++ and found that some of its functionality would be better written in Haskell. I've seen instructions on calling Haskell from C code, but is it possible to do the same with C++? EDIT: To clarify, what I'm looking for is a way to compile Haskell code into an external library that g++ can link with the ob...

How do I store value to string with RegOpenKeyEx?

I need to grab the path from the registry. The following code works except for the last part where I'm storing the path to the string. Running the debugger in Visual Studio 2008 the char array has the path, but every other character is a zero. This results in the string only being assigned the first letter. I've tried changing char res[1...

Type of pointer to member from base class

I have a problem regarding member pointers. The following code fails to compile using both Oracle Solaris Studio 12.2's CC and cygwin GCC 4.3.4 but works with Microsoft Visual C++ 2010: struct A { int x; }; struct B : public A { }; template<typename T> class Bar { public: template<typename M> void foo(M T::*p); }; int main(int, c...

Custom icon not displayed in upper left corner or on task bar

I have created a basic application with with windows api. It just displays a small window. I am starting from main function, getting the instance, created my windows class, etc. Everything works out fine. The problem I have however is that my custom icon will not display in the top left corner of the window or on the task bar, it just...

In Linux using C++ and GCC, is it possible to convert the virtual address to a physical address?

Under Linux, C++, and GCC, can I get a physical address for a given virtual address? I know I won't be able to manipulate the physical address as a physical address. ...

Help with writing to an I/O port in windows?

I'm trying to write a value to IO port 0x0466 in Windows. Normally, it is 0x0000. To change it, I can physically short-circuit two headers on the board, and the value changes to 0x0001. And it will stay 0x0001 even when I open the circuit, until I restart/etc. Now, the problem is, using software/IO port dll, I can write 0x0000 to port 0...

Combining string literals and integer constants

Given an compile-time constant integer (an object, not a macro), can I combine it with a string literal at compile time, possibly with the preprocessor? For example, I can concatenate string literals just by placing them adjacent to each other: bool do_stuff(std::string s); //... do_stuff("This error code is ridiculously long so I am g...

Writing a Hypervisor?

I am researching ideas for a PhD project. One of my thoughts is writing a hypervisor (or bare metal) (?) so I could run multiple OS's without use of a true host operating system. Example I get a menu of some type of options to start operating systems, view what the 'console' of what is going on in an OS that is already running. Reboot O...

C++/C# Screenshot on OpenGL Game ( Counter Strike 1.6) Problem

Hello , I developing a Antichat for counter strike using C++ .This AC need to take screenshots and upload them to a specified server. The problem is that in Win7/Vista the Screenshots are black. I made researches on web but didn't find something that helped me. I saw TAKSI sources, test all versions and Taksi 0.3 is good enough and not...

is all the available swig+python+mingw compile information outdated?

I'm trying to build a C++ extension for python using swig. I've followed the instructions below and the others to a T and can't seem to get my extension to load. I ran across this article on the MinGW site under "How do I create Python extensions?" http://www.mingw.org/wiki/FAQ I also found these tutorials: http://boodebr.org/main/p...

How do hidden structures work?

I notice in several API's, that you may create a struct which is used internally and has nothing. ex: ALLEGRO_BITMAP *bmp; ... bmp->(intellesense shows nothing) how do these types of structures work? are they simply handled like this internally? REAL_ALLEGRO_BITMAP *realbmp = (REAL_ALLEGRO_BITMAP*)bmp; or is there a cleaner soluti...

Variant * to string throws unknown exception.

I am using this code to sink events in a IWebBrowser2 webbrowser on c++: STDMETHODIMP AdviseSink::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, ...

Find and delete value in a vector

class Catalog { // string StationTitle; string StationLocation; public: string StationTitle; Catalog() { StationTitle = ""; StationLocation = ""; } Catalog(string Title, string Location) { StationTitle = Title; StationLocation = Location } void SetTitle(string Title) { StationTitle = Tit...