c++

C++ "const" keyword explanation

When reading tutorials and code written in C++, I often stumble over the const keyword. I see that it is used like the following: const int x = 5; I know that this means that x is a constant variable and probably stored in read-only memory. But what are void myfunc( const char x ); and int myfunc( ) const; ? ...

Find a string pattern in a set of strings

I have a set of strings, set<string>, and a string to check, I would like to do a binary search over it, obviously, a set is sorted. The purpose is to find the longer string in the set that is contained in the supplied string to check. It's basically to check for urls and registered handlers. I'm digging in the std algorithms and com...

Python Read and Output from Stdin Unbuffered

In C++ or any other languages, you can write programs that continuously take input lines from stdin and output the result after each line. Something like: while (true) { readline break if eof print process(line) } I can't seem to get this kind of behavior in Python because it buffers the output (i.e. no printing will happen ...

Can I use c++ to traverse DOM from C# BHO?

Hi! I'm writing a Browser Helper Object (BHO) for Internet Explorer in C#! I want to look for text nodes in the DOM, which requires me to traverse the whole DOM tree if I want to differentiate between text nodes and other types of nodes. This is supposedly time consuming in C#, but should be almost instantanious in C++ (?). Is there a...

C++: Is there a way to avoid redundancy with changing long declarations?

This has always bugged me with C and C++: In my header file I have something like: bool UpdateWindow( int w, int h, char bpp, bool force, char* someOtherLongStuff ) Now, if I want to change that for whatever reason, I have to do so in both header and source file - that's annoying in my opinion. Is there a way to automate this? I'm ...

Copy data from fstream to stringstream with no buffer?

Is there anyway I can transfer data from an fstream (a file) to a stringstream (a stream in the memory)? Currently, I'm using a buffer, but this requires double the memory, because you need to copy the data to a buffer, then copy the buffer to the stringstream, and until you delete the buffer, the data is duplicated in the memory. std:...

Increase in memory footprint. False alarm or a memory leak?

I have a graphics program where I am creating and destroying the same objects over and over again. All in all, there are 140 objects. They get deleted and newed such that the number never increases 140. This is a requirement as it is a stress test, that is I cannot have a memory pool or dummy objects. Now I am fairly certain there aren't...

Float, Double, Char, C++ Errors. What is wrong?

I am learning C++, but I ran into an error which I don't understand. Here is my source code, comments included (personal reference as I am learning.) #include "stdafx.h" #include <iostream> using namespace std; int main() { float h; //a float stands for floating point variable and can hold a number that is a fraction. I.E. 8.5 doub...

I want to make my own source code editor, what are the good choices to make ?

I've had it of those dozens of editors which never provides simple things like: distinguish type keywords and instruction keywords so I can put different colors on them. being cross platform using a standard GUI lib like qt gtk etc (notepad++, yes, I almost hate you). enough keyboard shortcut like duplicate line, comment selection, and...

How to pass arguments to a Policy's constructor?

In code: template<class T> struct FactorPolicy { T factor_; FactorPolicy(T value):factor_(value) { } }; template<class T, template<class> class Policy = FactorPolicy> struct Map { }; int _tmain(int argc, _TCHAR* argv[]) { Map<int,FactorPolicy> m;//in here I would like to pass a double value to a //FactorPo...

Strange error, set<int>::begin() always returning const iterator

Why is set.begin() always returning a const iterator and not a standard one? 35 int test(){ 36 std::set<int> myset; 37 myset.insert(2); 38 myset.insert(3); 39 int &res = *myset.begin(); 40 return res; 41 } test.cpp:39: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ ...

Odd output from a redefined << operator in c++

Hi all, I am having a runtime error that I can not for the life of me figure out. My program almost does what I want it to, but there are some corrupted characters that print out as gibberish. The program is supposed to take in a text file that represents a forest of trees, build the forest of trees, then traverse the forest to print i...

Understanding the '&' operator.

Hi. As of what I know about '&' operator, it returns the base address of the operand in memory. Let us imagine the following scenario (as on my machine): sizeof(int) = 4 bytes sizeof(float) = 4 bytes sizeof(char) = 1 byte Now, if I write something like this: void main() { int i = 5411; int *ip = &i; char *c = &i; printf("%d",*...

Sort char * With Special (german) Characters?

The problem I'm having is that I need to sort a whole bunch of char pointers, but they have special characters. I managed to get a sorting procedure like so: std::sort(dict_.begin(), dict_.end(), comp); bool comp(NumPair& a, NumPair& b) { return boost::lexicographic_compare(a.pFirst, b.pFirst); } This worked great, except that al...

itk filereader causes access violation exception

I have a problem with an Access Violation Exception. I am using itk and read a File with it's file reader. ThreeDImageFloatType* MyClass::loadImage(std::string filename){ const char* cfilename = filename.c_str(); fileReader = ImageFileReaderType::New(); fileReader->SetFileName(cfilename); try{ fileReader->Update(); }catch( ... ) ...

Not calling base class constructor from derived class.

Say I have a base class: class baseClass { public: baseClass() { }; }; And a derived class: class derClass : public baseClass { public: derClass() { }; }; When I create an instance of derClass the constructor of baseClass is called. How can I prevent this? ...

Confused with Overriding in C++

I'm trying to make a class inherits from other and override some methods. Classes 'header' is: class Objeto { public: virtual bool interseca(const Rayo &rayo, float magnitud); virtual bool breakNormal(const Punto &punto); virtual Vector normal(const Punto &punto); int idMaterial; }; class Esfera: public Obj...

Ellipse fit tangentally to a line modifying one axis

A graphic of this problem is here: http://dl.dropbox.com/u/13390614/Question.jpg Take an axis aligned ellipse with a fixed minor axis, and stretch the ellipse along its major axis till it becomes tangental to some line segment (A in the graphic). What are the coordinates of the tangental point (P), or, what would the major axis l...

Is c++ backwards-compatible with c?

Possible Duplicates: C subset of C++ -> Where not ? examples ? Should portable C compile as C++? Is every C program also valid C++? And is every C++ program also valid under c++0x? I seem to remember that there is a difference between how c and c++ handle enums that is not compatible, but don't remember what (or if) it is, o...

Doubles as template type?

Hi all, I am trying to create a generic class that handles ints, doubles, and strings. However, when trying to instantiate the template class with I get the following error message: error: 'double' is not a valid type for a template constant parameter The instantiation works completely fine with int types, as does the internal code,...