1,247 Posted Topics
Re: > how to read input from strings using cin 1. Create a [b]std::stringbuf[/b] which gets its input from a [b]std::string[/b]. 2.Set this as the [b]std::streambuf[/b] used by [b]std::cin[/b] [code]#include <iostream> #include <string> #include <sstream> int main() { std::string input_string = "string containing input 123 45 a 72.45 etc." ; std::stringbuf … | |
Re: The result of a [b]typeid[/b] expression is a [b]std::typeinfo[/b] object representing the *type* of the expression. Type of temp is an int, so [b]typeid(temp)[/b] and [b]typeid(4)[/b] both give [b]typeid(int)[/b]. To check if the user entered an int or not, check the state of the stream after the attempted input. If … | |
Re: [code=c++]std::ifstream file( "filename.txt" ) ; std::string line ; while( std::getline( file, line ) ) { // parse the line which was just read }[/code] | |
Re: Specify std::ios::ate as the file mode in the constructor. Or if the file is already open, close it and reopen with std::ios::ate as the file mode. | |
Re: you do not have to make one. it is available in libraries. either the boost string algorithms or the boost tokenizer could be used. here is an example using the boost string algorithms split. as you can notice, c++ libraries are far more powerful and expressive. in this example, we … | |
Re: A simple way: [code]struct A { static A* create() { return new A() ; } // ... private: A( /* ... */ ) ; A( const A& that ) ; // ... };[/code]If that is too simple for your requirements, see: [i]Item 27: Requiring or prohibiting heap-based objects[/i] in 'More … | |
Re: std::sort is an overloaded function, and this can cause problems with some compilers. see: [url]http://www.boost.org/doc/libs/1_37_0/libs/bind/bind.html#err_overloaded[/url] The work-arounds are simple; this is one way: [code]typedef std::vector<int>::iterator iterator ; void (*sort_function)( iterator, iterator ) = &std::sort ; thg.create_thread( boost::bind( sort_function, mass.begin(), mass.end() ) ;[/code] | |
Re: To export symbols from a shared library, you need to use the [b]__declspec(dllexport)[/b] attribute as you have done. A program that uses exported symbols from a DLL needs to import them using [b]__declspec(dllimport)[/b]. It is usually a good idea to have a single header file for the shared library, which … | |
![]() | Re: This is how I would do it. O(N log N) time. a. Keep track of the line number as the lines are read into a vector. b. Sort on the string (part that is to be checked for duplicates) c. Partition into unique and non-unique lines (on the string) d. … ![]() |
Re: This paper by Stroustrup addresses some of the issues raised in this thread. Well worth a read, IMHO. [url]http://www.research.att.com/~bs/new_learning.pdf[/url] | |
![]() | Re: > Size and speed aren't really an issue here, just ease of use. > Using std::strings as well would be beneficial. You might want to have a look at TinyXML. [url]http://www.grinninglizard.com/tinyxml/[/url] And perhaps TinyXML++, which leverages more C++ strengths. [url]http://code.google.com/p/ticpp/[/url] |
Re: > Is there a safe and legal way to go from a pointer to a member of an object > to a pointer to that same object? No. A pointer to a member is a pointer to a member of a class, not a pointer to a member of an … | |
Re: Define the static member variables that have been declared. [CODE] namespace fun{ // ... class driver{ // ... static int size; static int eff; static int idnumber; static std::vector<int> bmv; static std::vector<int> amv; static std::vector<int> bbv; static std::vector<int> abv; // ... }; // ... } // ... int fun::driver::size = … | |
Re: > error: ‘numeric_limits’ was not declared in this scope #include <limits> > error: ‘streamsize’ was not declared in this scope std::streamsize | |
Re: Yes, this is typically done using a monitor (aka condition variable). See: [url]http://en.wikipedia.org/wiki/Monitor_%28synchronization%29[/url] And: [url]https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables[/url] | |
Re: > But how do I go thru A folder and get all the files size's that are inside? See: [url]http://www.daniweb.com/forums/post342872.html#post342872[/url] That just prints the native_file_string. To get the file size, construct a [b]boost::filesystem::path[/b] from [b]iter->native_file_string()[/b] and then use [b]boost::filesystem::file_size[/b] on it, as in your current code. | |
Re: > Could you refer me to a site that explains exactly why it can't be initialized inside the class definition? I'd like to read up on the working behind why this is. [CODE]class Test { public: static const string MYARRAY[] ; // only a declaration };[/CODE] [CODE]class Test { public: … | |
Re: In this function [CODE]CEntityFactoryDictionary EntityFactoryDictionary( ) { static CEntityFactoryDictionary s_EntityFactoryDictionary; return s_EntityFactoryDictionary; } [/CODE] You are returning (by value) an anonymous temporary object which is a copy of the static s_EntityFactoryDictionary; you call InstallFactory on this anonymous temporary object which is immediately destroyed after that. Modify the function as: [CODE]CEntityFactoryDictionary[COLOR="Red"][B]&[/B][/COLOR] … | |
Re: 1. Declare each template friend function before the definition of template class. (#include of the header, as you have will do). 2. Add [b]<>[/b] in the friend declarations. [code]template <class T> class UList ; template <typename T > void sort( UList<T>& ) ; template <typename T > std::ostream& operator << … | |
Re: Write three 'main' functions, one in each file. [code] int ProgramList_main( int argc, char** argv ) ; int MD5Hash_main( int argc, char** argv ) ; int HazardCheck_main( int argc, char** argv ) ; [/code] Then write a single real main function, which calls these one after another: [code] int main( … | |
Re: Instead of defining it here, as [code] int map[ MAP_WIDTH * MAP_HEIGHT ] = { // ... }; [/code] Just declare it in this file [code] extern int map[] ; [/code] (And then define it in the implementation file of area, eg. area.cc) | |
Re: See [url]http://home.netcom.com/~chip.f/viterbi/tutorial.html[/url] And [url]http://www.eccpage.com/[/url] | |
Re: Well, you could start with these two and then follow the links given in them. [url]http://antivirus.about.com/od/whatisavirus/a/virussignature.htm[/url] [url]http://en.wikipedia.org/wiki/Anti-virus_software[/url] | |
Re: [ICODE]gethostname[/ICODE] will give you the name of the current host [ICODE]gethostbyname[/ICODE] looks up the host with a particular name and will give you the address > [ICODE]man 3 gethostname[/ICODE] > [ICODE]man 3 gethostbyname[/ICODE] | |
Re: [code]void Example::function1( const Example& ex ) { Example copy_of_ex( ex ) ; /* whatever */ }[/code] | |
Re: Windows operating systems: Write your program and run it as an NT service [url]http://www.commsoft.com/services.html[/url] Unix and unix like operating systems: Daemonize your program and run it as a unix daemon. [url]http://www.enderunix.org/docs/eng/daemon.php[/url] | |
Re: Change line 32 to use [b]fabs[/b] instead of [b]abs[/b] (why?) [code]while ( fabs(high - low) > epsilon ) { [/code] Ideally, also change the type of all the floating point numbers to [b]double[/b] instead of [b]float[/b] (why?) And you should be ok. | |
Re: [code]class cIsBad : public [color=red][b]std::[/b][/color]unary_function <T, bool> {[/code] | |
Re: use [b]std::find_if[/b] to locate the right position to insert the new item. For example: [code]#include <iostream> #include <functional> #include <algorithm> #include <list> #include <iterator> struct Ctest { Ctest( double fv ) : fVal(fv) /* .... */ { /* ... */ } // ... [color=red][b]bool operator > ( const Ctest& that … | |
Re: Are you looking for something like this? [code]template< typename T > struct list { T* current ; }; template< typename T, T* T::*PTRMEMBER > T* get_next( list<T>& lst ) { lst.current = lst.current->*PTRMEMBER ; return lst.current ; } struct A { A* member_one ; A* member_two ; }; int main() … | |
Re: there is no readily available facility in the standard library to do this. however, the library is designed to be extensible: so we can easily write our own stream/streambuf derived classes. the Boost.Iostreams library does contain stream/streambuf derived classes which use a file descriptor/HANDLE. Boost is widely portable, so we … | |
Re: you could try ncurses [url]http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/intro.html#WHATCANWEDO[/url] | |
Re: See: [url]http://docs.sun.com/source/806-3568/ncg_goldberg.html[/url] You might want to use a library like MPFR (a multiple-precision floating-point computations library with correct rounding). A C++ wrapper would be convenient. There are several, you can find one of them here: [url]http://beshenov.ru/mpfrcpp/[/url] | |
Question posed by josolanes: [quote]I looked at your thread here: [url]http://www.daniweb.com/forums/thread88255.html[/url] from a couple years back And I'm starting to get a grasp of your first post. I understood how to do the factorial struct really quickly, but the _if and _for statements still make me feel uneasy, though I … | |
Re: > Its printing only the first elements Well, you have a multimap which contains multimaps : [b]multimap<string,multimap<string,int> > mymm;[/b] So you need a nested loop to print the contents of the inner multimaps. Something like: [code]for (itx = mymm.begin(); itx != mymm.end(); ++itx) { cout << " [" << itx->first … | |
Re: Couple of general points: In general use references (instead of pointers) for strings, streams and the like. Use const where ever and whenever possible - it is your friend. Prefer using '\n' to std::endl to send a new line to a stream, unless your intent is to also flush the … | |
Re: Export members selectively from your dll: [code]class my_class { public: void foo() ; // not exported __declspec(dllexport) void bar() ; // exported }; void my_class::foo() {} __declspec(dllexport) void my_class::bar() {}[/code] And import them selectively in places where you want to use it: [code]class my_class { public: void foo() ; // … | |
Re: Use something like [b]std::vector< boost::any >[/b] [url]http://www.boost.org/doc/libs/1_37_0/doc/html/any.html[/url] | |
Re: Either of these two is the right way to declare C::foo<> as a friend. [code]struct C { template < bool B > void foo() ; template< bool B > class B { // ok, the compiler already knows that C::foo // is a template function friend void C::foo() ; // … | |
Re: > What is this thingy that I'm trying to name or whatever? Well, it is part of the IS, under section 7.1.5.1 'The cv-qualifiers' [quote]A variable of const-qualified integral or enumeration type initialized by an integral constant expression can be used in integral constant expressions - IS 7.1.5.1/2 A pointer … | |
Re: alternatively, [code] std::ifstream file("filename") ; std::istream_iterator<std::string> begin(file), end ; if( find( begin, end, "Computer" ) != end ) // found [/code] | |
Re: Rewrite [inlinecode]void Matrix::Multiply(Matrix b) ;[/inlinecode] as [inlinecode]Matrix Matrix::Multiply( const Matrix& b ) const ;[/inlinecode] In the implementation, do not modify either of the two Matrix objects involved in the multiply. Construct a new result Matrix object of the appropriate size, populate it and return it. Ok, I see that Dragon … | |
Re: A good implementation of the vector would call the copy constructor when memory is reallocated. Invoking the default constructor first and then the assignment operator has an efficiency issue. Note: C++0x containers would use move semantics (instead of copy semantics) if they are available on the object. Modify the code … | |
Re: [b]std::transform[/b] is perhaps what you are looking for. [url]http://www.cppreference.com/wiki/stl/algorithm/transform[/url] | |
Re: > obj.baseInfo::SlotCount[0] = 5; > This compiles fun but gives me an error after running it. It should not compile (though IIRC, microsoft compilers just give a warning that memory would be corrupted and proceed to generate incorrect code that will corrupt the memory - on the stack in this … | |
Re: Most, but not all, Boost libraries are header-only libraries; most often, there's nothing to build. However, a few libraries must be built separately. Boost DateTime has a binary component that is needed if you're using its to_string/from_string or serialization features. If you do not have the binaries installed, you need … | |
Re: In general, if you're displaying error messages to the user based on what() of an exception that was thrown, you are probably doing something wrong. The code that throws an exception is very unlikely to be able to compose a relevant and user-comprehensible error message at the point an exception … | |
Re: [quote=joeprogrammer;340533] [inlinecode]endl[/inlinecode] is not a character, it's a macro which: a) inserts a newline '\n' into the output buffer b) flushes the output buffer. [/quote] endl is a function [code] template<typename CHARTYPE, typename TRAITS> [URL="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/classstd_1_1basic__ostream.html"][/URL] basic_ostream<CHARTYPE,TRAITS >& endl( basic_ostream<CHARTYPE,TRAITS>& stm ) { stm << '\n' ; returm stm.flush() ; } … | |
Re: modify the signature of these two functions: [code]double Value_Of_Num( [COLOR="Red"]const[/COLOR] string &Expr ) ; double Value_Of_Expr( [COLOR="Red"]const[/COLOR] string &Expr ) ;[/code] or a comforming compiler will give you errors. you can't initialize a reference to a modifiable value with an anonymous temporary (deemed to be a constant). > The problem … |
The End.