1,247 Posted Topics
Re: > I wrote a spare Matrix class and the Matrix was stored in a coornidate format. Consider using a data structure that would allow efficient look up of the element at a given row and col position. One way is to use a std::map<> where the (row,col) tuple is the … | |
Re: The architecture is fairly straightforward. The stream class (in case of [ICODE]std::cout[/ICODE], [ICODE]std::ostream[/ICODE]) is responsible for parsing and formatting the sequence of characters required for output. The stream buffer object associated with the stream provides an abstract connection to some external device; the stream buffer is responsible for transport of … | |
Re: Use a library, perhaps? zlib is the library of choice - open source with a non-viral license, proven, reliable and portable. [url]http://www.zlib.net/[/url] You might want to use a C++ wrapper for zlib. Very many are available; for example [url]http://code.google.com/p/zipstream/[/url] [url]http://www.cs.unc.edu/Research/compgeom/gzstream/[/url] | |
Re: Straddling compile-time techniques and run-time techniques requires a fair amount of care. The code tends to be fairly stylized. For this paricular example: [CODE]#include <iostream> #include <boost/any.hpp> #include <vector> #include <boost/mpl/list.hpp> #include <boost/mpl/for_each.hpp> using namespace boost ; struct triangle { void draw() const // polymorphic across many different types { … | |
Re: The K-means clustering algorithm computes yields K clusters, where K is given by you. You have to "tell" the algorithm beforehand how many clusters the data set should be broken up into. A hierarchical clustering algorithm on the other hand forms successive groupings, with the number of clusters varying from … | |
Re: [B]list[/B] is a typedef (an alias for) a pointer to a [B]nodo_lista[/B]. [ICODE]typedef struct nodo_lista *list;[/ICODE] If struct [B]nodo_lista[/B] does have a member function [B]insert_list()[/B], then: [ICODE](i)->primerapos[COLOR="Red"][B]->[/B][/COLOR]insert_list(&primerapos, contador_etiquetas);[/ICODE] | |
Re: Though the standard does not say anything specific about the representation of a [B]wchar_t[/B], it is either strictly Unicode (UCS-2) or ISO 10646 (UCS-4) on every implementation. These two have an identical character repertoire and code points for the Basic Multilingual Plane. In practice, this will remove non-printable characters from … | |
Re: > I know I could try getline(cin, FullName), then split it them all > but the text did not recommend this and suggesting 3 string variables This would be one way of doing it: [CODE] std::string FirstName, LastName ; std::cout << "What is your first and last name? " ; … | |
Re: > tokenize a string, yet allow it to leave the whitespaces as token also. Construct a [ICODE]boost::char_separator<char>[/ICODE] with white spaces as the kept delimiters (an an empty string for dropped delimiters). Tokenize with a tokenizer using this as the tokenizer function. For example: [CODE]#include <iostream> #include <boost/tokenizer.hpp> int main() { … | |
Re: See [url]http://en.wikipedia.org/wiki/Cyclic_number#Construction_of_cyclic_numbers[/url] | |
Re: To get a random int between 0 and 31 (inclusive) using rand(), prefer something like [ICODE]int( 32.0 * ( rand() / (RAND_MAX+1.0) ) ) ;[/ICODE] Or else, you would be assuming that the least significant 5 bits of the pseudo random number generated by rand() are also randomly distributed. | |
Re: [CODE]string parseInputString( const string& oneline ) // prefer passing by const referance { // when possible, initialize objects at the point of their definition string::size_type index = oneline.find (' ') ; string firstName = oneline.substr( 0, index ) ; // find the next non-space character index = oneline.find_first_not_of( ' ', … | |
Re: Have you overloaded the operator for the base class? That is, do you have [ICODE]std::ostream & phy_namespace::operator<<( std::ostream &cout, const student& student);[/ICODE] Also, do you know what virtual functions are? | |
Re: Some kind of a radix tree (a patricia trie is commonly used). | |
Re: [CODE]v.assign( 1U, 4 ) ; // usually more efficient than create temporary vector and then swap v = { 4 } ; // only in C++ 2011[/CODE] | |
Re: [CODE]typedef A::B B ; // ...[/CODE] | |
Re: Something like this: [CODE] name = fName + ":" + lName; // find the first free position int free_pos = -1 ; for(int i = 0; i < size; i++) if( nameList[i].length() == 0 ) { free_pos = i ; break ; } if( free_pos != -1 ) nameList[free_pos] = … | |
Re: Use boost::filter_iterator, perhaps. [url]http://www.boost.org/doc/libs/1_46_0/libs/iterator/doc/filter_iterator.html[/url] A trivial example: [CODE]#include <vector> #include <algorithm> #include <boost/iterator/filter_iterator.hpp> #include <iostream> int main() { std::vector<int> seq = { 36, 4, 63, 21, 41, 64, 0, 35, 4, 17, 8, 33 } ; auto is_odd = [] ( int i ) { return i%2 != 0 ; … | |
Re: Declaration [CODE]void Resultant(const double[], const double[], const double[], [COLOR="Red"][B]const[/B][/COLOR] double[], const int, double&, double&, double&);[/CODE] does not match the definition [CODE]void Resultant(const double M[], const double X[], const double Y[], [COLOR="Red"][B]/* ??? */[/B][/COLOR] double Z[], const int moves, double& Rx, double& Ry, double& Rz) { // ...[/CODE] Not a great … | |
Re: > my compilers::tdm gcc4.5.2 You have a current C++ compiler; prefer using [B]std::bind[/B] over [B]boost::bind[/B] (compile with [B]-std=c++0x[/B]) > I don't know how to solve the overloaded problem like this one [B]boost::bind[/B] or [B]std::bind[/B] have no good way to figure out which overload you intended to bind. You have to … | |
Re: Use std::move to convert the lvalue reference to an rvalue reference. see: [url]http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2027.html#Move_Semantics[/url] > Maybe this? [ICODE]testPF(std::move(a), std::move(b) ) ;[/ICODE] Yes. | |
Re: The second argument to [B]RegisterHotKey()[/B] is the id of the hot key. If multiple [B]WM_HOTKEY[/B] messages are received by the same thread, you need to examine the lParam of the message for the id of the hot key that was pressed. [CODE]enum { F9_KEYID = 1, F10_KEYID = 2 } … | |
Re: If the intent was to use std::copy and stream iterators, I have nothing to add. If the intent was to write a function to copy a file, this would be easier and also more efficient. [CODE]bool copy_file( const char* input_file, const char* output_file ) { return std::ofstream( output_file ) << … | |
Re: Make mean and variance droppable accumulators. [url]http://www.boost.org/doc/libs/1_37_0/doc/html/accumulators/user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extending_the_accumulators_framework.defining_a_new_accumulator.droppable_accumulators[/url] | |
Re: [CODE]int main() { int a, b; char c ; // ... ins >> a >> c >> b; cout <<a <<b; }[/CODE] | |
Re: There are many different ways to do this. Perhaps, the simplest is this: [CODE]#include <fstream> #include <set> #include <string> #include <iterator> #include <algorithm> #include <iostream> template< typename T > std::set<T> file_to_set( const char* path ) { std::ifstream file(path) ; return std::set<T>( std::istream_iterator<T>(file), std::istream_iterator<T>() ) ; } int main() { const … | |
Re: Perhaps this might help: [url]http://curl.haxx.se/mail/archive-2005-10/att-0119/DevCpp-Mingw_Install___Compilation_Sept_2005.rtf[/url] | |
Re: You could also use a stringstream to do the conversion. [CODE]typedef unsigned char byte ; std::vector<byte> to_byte_sequence( const std::string& str ) { std::vector<byte> result ; std::istringstream stm(str) ; stm >> std::hex >> std::noshowbase ; unsigned int b ; while( stm >> b ) result.push_back(b) ; if( stm.eof() ) return result … | |
Re: [CODE]class UpperCaseMonitor : [COLOR="Red"][B]public[/B][/COLOR] Observable { // ...[/CODE] | |
Re: [QUOTE]I've found that often in my code I have a need for primitive variables protected with synchronization objects such as a boost::mutex[/QUOTE] If by primitive variables you mean variables of types like int, double, pointer and so on, using heavy weight synchronization (using mutexes and so on) as a general … | |
Re: If the number of elements is large and the size of each element is small (eg. an array of int), the well known reverse three times technique seems to be the most efficient. [CODE]void reverse( int a[], std::size_t N ) { for( std::size_t i = 0 ; i < N/2 … | |
Re: see: [url]http://www.daniweb.com/software-development/cpp/threads/139239[/url] | |
Re: A simple, reasonably fast algorithm to find the smallest prime factor of a number N would be: a. generate all prime numbers up to the square root of N using a sieve algorithm. b. the first (smallest) generated prime number that divides N is the smallest prime factor. For example: … | |
Re: If the number of vectors which have elements on which you want to perform a particular operation (for example, print it) is fixed, you could write a simple wrapper function: [CODE]#include <algorithm> template< typename SEQ1, typename SEQ2, typename SEQ3, typename FUNCTION > inline void for_each( SEQ1& seq1, SEQ2& seq2, SEQ3& … | |
Re: This is not Kadane's algorithm, is it? This algorithm makes multiple passes through the sequence including nested passes: [CODE]#include <limits> #include <utility> for (int j1=0;j1<=arraysize; j1++) // O(N) { partial_sum(param2+j1,param2+arraysize,newSeq1+determine_start1); // O(N) for (int cycle_thru=0;cycle_thru<= length( newSeq1 ) ;cycle_thru++) // O( N*N) { // ... } // ... }[/CODE] Incidentally, … | |
Re: Use [b]inet_pton()[/b] in [b]<arpa/inet.h>[/b] (Unix) or [b]Ws2tcpip.h[/b] (Windows Vista+) [url]http://www.kernel.org/doc/man-pages/online/pages/man3/inet_pton.3.html[/url] to convert the IPv6 address string and place it in a [b]in6_addr[/b] struct. This struct has a union where the address can be viewed as an array of 16 octets. [code] const std::string& address_string = "fe80::202:b3ff:fe1e:8329" ; in6_addr address ; … | |
Re: Use a discriminated union. [CODE]struct int_or_char { int_or_char( int ii = 0 ) : type(INT), i(ii) {} int_or_char( char cc ) : type(CHAR), c(cc) {} enum type_t { INT, CHAR } ; type_t type ; union { int i ; char c ; }; // etc. };[/CODE] and now: [ICODE]Stack< … | |
Re: The general rule is that a file in a directory named 'temp' or 'tmp' is a temporary file; one that is required only for the duration of the execution of the program that uses it. By convention, these can be safely deleted; no reasonable program is expected to break if … | |
Re: [QUOTE]I have a global map (probably a bad idea) and I want to explicitly free it's used memory The functions clear() only removes all elements, it doesn't free any memory. [/QUOTE] Something like this, perhaps: [CODE]template < typename KEY, typename DATA, typename CMP_KEYS = std::less<KEY>, typename ALLOCATOR = std::allocator< std::pair<const … | |
Re: [code]#include <iterator> #include <string> struct String_list { struct iterator : std::iterator< std::bidirectional_iterator_tag, std::string > { explicit iterator( std::string* p ) : ptr(p) {} std::string& operator*() { return *ptr ; } // ... iterator& operator++ () { ++ptr; return *this ; } // ... // and so on for the other … | |
Re: The [b]catch()[/b] does not match the type of the exception that is thrown. Either: [code] try { std::ifstream somefile(argv[1], std::ios::in|std::ios::binary ) ; if( !file ) throw "Error opening file!" ; // no error // ... } catch( const char* cstr ) { std::cerr << cstr << '\n' ; }[/code] or … | |
Re: [QUOTE=;]You are basically asking the person who reads your code to be aware of "value-initialization of POD type means zero-initialization". This is not so common (many people wouldn't even know the difference between default-initialization and value-initialization).[/QUOTE] I would expect that anyone who has used a standard library container like std::vector<> … | |
Re: There is a typo on line 74. Should be [icode]my_size = size ;[/icode] Also, in your [b]operator=()[/b], you need to check for a trivial self-assignment. [url]http://yosefk.com/c++fqa/assign.html[/url] | |
Re: For a dynamically allocated array with [icode]new T[N][/icode], N must be non-negative; it may evaluate to zero. When N is zero, an array with no elements is allocated and a distinct non-null pointer to it is returned. This array with no elements is deleted normally; with [icode]delete[][/icode] | |
Re: std::partition with a predicate could be used to split the sequence. [code=c++]#include <iostream> #include <algorithm> #include <functional> #include <iterator> #include <vector> #include <cstdlib> #include <ctime> using namespace std ; template< typename ITER > inline void column_print( ITER beginc1, ITER endc1, ITER beginc2, ITER endc2 ) { while( (beginc1!= endc1) || … | |
Re: Consider making your print functions const-correct. [icode]void cProg3::Print_s() const[/icode] etc. There are many ways to print out the elements in an STL sequence. You could iterate through the sequence (as you have done). You could use a standard algorithm along with a function object (or a lambda function if you … | |
Re: Just create a console application (linker option /SUBSYSTEM:CONSOLE) and write your normal gui code. (Both use the same Win32 or Win64 environment subsystem). You will have a Console created for you and a main() with stdin, stdout and stderr initialized correctly. Use GetModuleHandle(), GetStartupInfo() etc. if you need the parameters … | |
Re: You can check if a string is a palindrome without having to make a copy; just iterate from either end looking for a mismatch. [code=c++]bool is_palindrome( const std::string& str ) { std::string::const_iterator mid = str.begin() + str.size() / 2 ; return !str.empty() && std::mismatch( str.begin(), mid, str.rbegin() ).first == mid … | |
Re: [code] int numerator, denominator ; char slash ; // std::cin >> std::skipws // this is the default if( ( std::cin >> numerator >> slash >> denominator ) && ( slash == '/' ) ) { // ok } else { // error in input }[/code] |
The End.