1,247 Posted Topics

Member Avatar for ztdep

> 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 …

Member Avatar for vijayan121
0
189
Member Avatar for kadhaipaneer
Member Avatar for lochnessmonster

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 …

Member Avatar for vijayan121
0
120
Member Avatar for Nor96x

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]

Member Avatar for vijayan121
0
92
Member Avatar for stereomatching

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 { …

Member Avatar for thekashyap
0
161
Member Avatar for Jsplinter

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 …

Member Avatar for Jsplinter
0
258
Member Avatar for montjoile

[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]

Member Avatar for montjoile
0
170
Member Avatar for dboltz03

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 …

Member Avatar for vijayan121
0
2K
Member Avatar for Saith

> 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? " ; …

Member Avatar for vijayan121
0
307
Member Avatar for gregarion

> 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() { …

Member Avatar for vijayan121
0
413
Member Avatar for ronnieaka
Member Avatar for ronnieaka
0
209
Member Avatar for miskeen

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.

Member Avatar for vijayan121
0
227
Member Avatar for andimiami

[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( ' ', …

Member Avatar for andimiami
0
122
Member Avatar for davelee

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?

Member Avatar for vijayan121
0
864
Member Avatar for geekme
Member Avatar for vijayan121
0
44
Member Avatar for tikoti

[CODE]v.assign( 1U, 4 ) ; // usually more efficient than create temporary vector and then swap v = { 4 } ; // only in C++ 2011[/CODE]

Member Avatar for tikoti
0
172
Member Avatar for Jsplinter
Member Avatar for rannamaa

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] = …

Member Avatar for rannamaa
0
117
Member Avatar for fandango

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 ; …

Member Avatar for fandango
0
377
Member Avatar for Tinee

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 …

Member Avatar for Tinee
0
107
Member Avatar for stereomatching

> 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 …

Member Avatar for stereomatching
0
364
Member Avatar for stereomatching

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.

Member Avatar for stereomatching
0
109
Member Avatar for triumphost

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 } …

Member Avatar for triumphost
0
137
Member Avatar for Ancient Dragon

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 ) << …

Member Avatar for vijayan121
0
717
Member Avatar for stereomatching

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]

Member Avatar for vijayan121
0
878
Member Avatar for pmark019

[CODE]int main() { int a, b; char c ; // ... ins >> a >> c >> b; cout <<a <<b; }[/CODE]

Member Avatar for pmark019
0
1K
Member Avatar for karan_kanna

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 …

Member Avatar for csurfer
0
108
Member Avatar for bigwhiteegg

Perhaps this might help: [url]http://curl.haxx.se/mail/archive-2005-10/att-0119/DevCpp-Mingw_Install___Compilation_Sept_2005.rtf[/url]

Member Avatar for vijayan121
0
248
Member Avatar for M3SSIAH

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 …

Member Avatar for M3SSIAH
0
306
Member Avatar for jfunchio
Member Avatar for jfunchio
0
171
Member Avatar for pseudorandom21

[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 …

Member Avatar for vijayan121
0
259
Member Avatar for Veneficvs

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 …

Member Avatar for vijayan121
0
2K
Member Avatar for hawita

see: [url]http://www.daniweb.com/software-development/cpp/threads/139239[/url]

Member Avatar for hawita
0
138
Member Avatar for Despairy

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: …

Member Avatar for Despairy
0
1K
Member Avatar for mclemon

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& …

Member Avatar for vijayan121
0
103
Member Avatar for Coffee_Table

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, …

Member Avatar for Coffee_Table
0
247
Member Avatar for gazsroeposc

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 ; …

Member Avatar for gazsroeposc
0
2K
Member Avatar for SourabhT

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< …

Member Avatar for mike_2000_17
0
164
Member Avatar for mrnobody

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 …

Member Avatar for vijayan121
0
338
Member Avatar for tomtetlaw

[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 …

Member Avatar for Rashakil Fol
0
6K
Member Avatar for mitrious

[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 …

Member Avatar for vijayan121
0
677
Member Avatar for TheFueley

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 …

Member Avatar for TheFueley
0
8K
Member Avatar for lochnessmonster

[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<> …

Member Avatar for vijayan121
0
157
Member Avatar for failbot

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]

Member Avatar for vijayan121
0
2K
Member Avatar for lochnessmonster

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]

Member Avatar for template<>
0
90
Member Avatar for purepecha

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) || …

Member Avatar for arkoenig
-1
1K
Member Avatar for atticusr5

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 …

Member Avatar for atticusr5
0
134
Member Avatar for spoonlicker

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 …

Member Avatar for peter_budo
0
2K
Member Avatar for jimJohnson

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 …

Member Avatar for jimJohnson
0
224
Member Avatar for rickster11

[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]

Member Avatar for vijayan121
0
121

The End.