Sunday, June 30, 2013

Quicksort done. Can I get a check on it?

Newsgroup: comp.lang.c++

Subject: Re: Quicksort done. Can I get a check on it?

From: Christopher Pisz <cpisz@...>

Date: Sun, 30 Jun 2013 18:39:23 -0500



On 6/30/2013 1:15 PM, Christopher Pisz wrote:

> So, I'll try a few using plain old array and come back if I get stuck.



I think I've implemented a Quicksort after much Googling. I based it

upon someone elses and walked through it on paper a few times. I am not

too certain this meets the criteria. In debugging it, it seems to sort

the given collection upon the first frame of recursion, yet it keeps

calling itself anyway. When it does call itself again, it just goes

through the checks and calls itself again, and goes through the

checks... So I had a stack 5 deep or an equal number of calls as there

were elements. Perhaps I am confused. Should there not be a way to

determing, "Hey we are sorted!, all done!" I cannot claim to follow it

completely. It is one of those fuzzy implementations that is sinking in.



Here is what I have:



//-------------------------------------------------------------------------------

#include <algorithm>

#include <iostream>

#include <vector>



//-------------------------------------------------------------------------------

/// Quick Sort

/// Choose a pivot point and remove it from the collection

/// For each element in the collection, if it is less than the pivot

than add it to another collection 'less', otherwise add it to another

collection 'greater'

/// Perform quicksort recursively on 'less' and 'greater' and

concatentate the results as 'less' 'pivot' 'greater'

/// Best Case Complexity O(nlogn)

/// Average Complexity O(nlogn)

/// Worst Case Complexity O(n^2)



size_t QuickSortPartition(unsigned int * collection, size_t beginIndex,

size_t endIndex)

{

unsigned int pivotValue = collection[beginIndex];

size_t pivotIndex = beginIndex;



while( pivotIndex < endIndex )

{

while( pivotValue < collection[endIndex] &&

endIndex > pivotIndex )

{

--endIndex;

}



std::swap(collection[pivotIndex], collection[endIndex]);



while( pivotValue >= collection[pivotIndex] &&

pivotIndex < endIndex )

{

++pivotIndex;

}



std::swap(collection[endIndex], collection[pivotIndex]);

}



return pivotIndex;

}



void QuickSort(unsigned int * collection, size_t beginIndex, size_t

endIndex)

{

// Nothing to do when container is empty or only has one element

if( beginIndex >= endIndex )

{

return;

}



size_t pivotIndex = QuickSortPartition(collection, beginIndex,

endIndex);



if( pivotIndex > beginIndex)

{

QuickSort(collection, beginIndex, pivotIndex - 1);

}



if( pivotIndex < endIndex )

{

QuickSort(collection, pivotIndex + 1, endIndex);

}

}



//------------------------------------------------------------------------------

void Display(unsigned int * begin, size_t size)

{

for(size_t index = 0; index < size; ++index, ++begin)

{

std::cout << ' ' << *begin;

}

std::cout << '\n';

}



//------------------------------------------------------------------------------

/// Test driver

int main()

{

unsigned int collection[] = {5,2,4,1,3};



// Quick sort

QuickSort(collection, 0, sizeof(collection) / sizeof(unsigned int)

- 1);

Display(collection, sizeof(collection) / sizeof(unsigned int));



system("pause");

return 0;

}











via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?32342-Quicksort-done-Can-I-get-a-check-on-it&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Difference between pointer to the no data member and the pointer to the first member

Newsgroup: comp.lang.c++

Subject: Difference between pointer to the no data member and the pointer to the first member

From: somenath <somenathpal@...>

Date: Sun, 30 Jun 2013 09:25:24 -0700 (PDT)







I am trying to understanding the difference between the pointer to the no data member and pointer to the first member of class.



Also I have learned that ?The value returned from taking the member?s address, however, is always bumped up by 1?.

To understand this concept I wrote the following simple program.



#include<iostream>

using namespace std;

class A

{

public:

int data;

int data2;



};



int main(void)

{



A a1;

int A::*first_data_member =&A::data;

int A::*second_data_member =&A::data2;

int A::*no_data_member ;

cout<<"no_data_member = "<<(no_data_member)<<endl;

cout<<"first_data_member = "<<(first_data_member)<<endl;

cout<<"second_data_member = "<<(second_data_member)<<endl;





return 0;



}



The output of the above program is as follows.

=================================================

no_data_member = 1

first_data_member = 1

second_data_member = 1



================================================



I am not able to understand the output of the program.



My expectation is if no_data_member is 1 then first_data_member sould be = no_data_member + 1 and second_data_member should be = first_data_member +1.



Please let me know where I am going wrong?













via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?32173-Difference-between-pointer-to-the-no-data-member-and-the-pointer-to-the-first-member&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

sorts and iterators

Newsgroup: comp.lang.c++

Subject: sorts and iterators

From: Christopher Pisz <cpisz@...>

Date: Sun, 30 Jun 2013 10:50:25 -0500



I am brushing up for an upcoming interview. They let me know beforehand

that they will be asking about sorting algorithms and expect me to write

some code. They let me know I should study, so I am studying :)



I know the old college academic exercises where I can make a struct that

represents a node and implement my own linked list, etc. etc. I'd like

to try some custom sorts and performance time them like I would in the

real world. So, I ask myself, how would I do it in the real world?



I would most likely have some stl container or a class that implements

iterators. I am also aware the stl has a build in sort with O(nlog(n))

complexity.



So, how do I go about implementing a sort that uses iterators? I am not

really sure where to start.



I did some Googling and see stl sort requires swap and a move copy. I

believe these are new c++13 features? Can we assume I am still using the

2003 standard for now and then try again using C++13?



My current job was using really out of date tools and I haven't had the

chance yet to catch up on the new standard.









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?32152-sorts-and-iterators&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Eliminate second invocation of class (example_visitor) object

Newsgroup: comp.lang.c++

Subject: Eliminate second invocation of class (example_visitor) object

From: Mark <ma740988@...>

Date: Fri, 28 Jun 2013 20:43:50 -0700 (PDT)



Code referenced below compiles and runs. The issue I'm faced with is the constructor for example_visitor is invoked twice. The second invocation of the example_visitor constructor is predicated on invocation of dispatch in particular the line: typename Type::value_type visitor ; within dispatch



What modifications needs to occur such that I could use the 'this' pointer from the first instance of example_visitor in dispatch? I tried modifying the code to have example_visitor derive off dispatch but to no avail.



Sample appreciated. Thanks in advance.





# include <map>

# include <boost/variant.hpp>

# include <boost/mpl/vector.hpp>

# include <boost/mpl/contains.hpp>

# include <boost/utility/enable_if.hpp>

# include <boost/lexical_cast.hpp>

# include <boost/variant.hpp>

# include <boost/any.hpp>

# include <boost/shared_ptr.hpp>



# include <vector>

# include <string>

# include <iostream>



// Generic visitor

template <typename Visitor, typename TypeList>

struct generic_visitor :

public boost::static_visitor<void>,

public Visitor

{

template <typename T>

inline void operator ()

( T& v, typename boost::enable_if<

typename boost::mpl::contains< TypeList, T >::type >::type *dummy = NULL )

{

Visitor::operator () (v);

}



template <typename T>

inline void operator ()

( T& v,

typename boost::disable_if <

typename boost::mpl::contains< TypeList, T >::type >::type *dummy = NULL )

{}

};



class nil {

protected :

nil(const nil& );

nil& operator = (const nil& ) ;

unsigned int abc ;

public :

unsigned int const get() { return abc ; }

nil ()

: abc ( 5 )

{}

};



template < typename Type >

class dispatch {



typedef boost::variant <

nil&, char&, int&, double&

> sql_field;

typename Type::value_type visitor ;



private :

int test_int ;

double test_double ;



public :

dispatch ()

: test_int ( 1 )

, test_double ( 1. )

{}



template < typename T >

void doit ( T& in ) {

std::cout << "(typeid=" << typeid( in ).name() << std::endl;

typename dispatch< Type >::sql_field obj ( in );

boost::apply_visitor ( this->visitor, obj );

}



};



struct example_visitor

{

typedef generic_visitor

< example_visitor,

boost::mpl::vector<nil, char, int > > value_type;

typedef dispatch < example_visitor > dispatch_evisit ;





dispatch_evisit* ptr ;

int x ;

nil n ;



example_visitor()

: ptr ( 0 )

, x ( 4 )

{ std::cout << "." ; }



void operator () (char& v) {

std::cout << "character detected" << std::endl;

}



void operator () (int& v) {

std::cout << "(integer detected=" << v << std::endl;

}



void operator () (nil& v) {

std::cout << "nil detected=" << v.get() << std::endl;

}

void initialize () {

ptr = new dispatch_evisit ;

}

void execute () {

if ( ptr ) {

ptr->doit ( x ) ;

ptr->doit ( n ) ;

x += 2 ;

}

}

};





int main() {



example_visitor test;

test.initialize();

for ( unsigned int odx ( 0 ); odx < 10; ++odx ) {

test.execute() ;

}

std::cin.get();

}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?31323-Eliminate-second-invocation-of-class-(example_visitor)-object&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Saturday, June 29, 2013

Question regarding the size of the class

Newsgroup: comp.lang.c++

Subject: Question regarding the size of the class

From: somenath <somenathpal@...>

Date: Fri, 28 Jun 2013 23:29:56 -0700 (PDT)





I am not able to understand why the sizeof the class B is 8

I have the following program.

#include<iostream>

using namespace std;



class X {

};

class Y : public virtual X {};

class Z : public virtual X {};

class A : public Y, public Z {};

int main (void)

{

cout<<"Sizeof int = "<<sizeof(int)<<endl;

cout<<"Sizeof void* = "<<sizeof(void*)<<endl;

cout<<"Sizeof int* = "<<sizeof(int*)<<endl;

cout<<"Size of X = "<<sizeof (X)<<endl;

cout<<"Size of Y = "<<sizeof (Y)<<endl;

cout<<"Size of Z = "<<sizeof (Z)<<endl;

cout<<"Size of A = "<<sizeof (A)<<endl;

return 0;

}

Output

Sizeof int = 4

Sizeof void* = 8

Sizeof int* = 8

Size of X = 1

Size of Y = 8

Size of Z = 8

Size of A = 16



According to my understanding the sizeof class Y would be sum of sizeof(class X) + sizof(virtual_table_pointer) + sizeof padding for alignment required.



So in this case sizeof(class X) = 1 + sizeof(virtual_table_pointer) = 8 (not sure about this ) + size of padding for alignment requirement ( 3) = 12



So I am not able to understand why the sizeof y is printed as 8?



Now when I change the program as the following way (introduce one public variable in class X)

#include<iostream>

using namespace std;



class X {

public:

int x;



};

class Y : public virtual X {};

class Z : public virtual X {};

class A : public Y, public Z {};



int main (void)

{

cout<<"Sizeof int = "<<sizeof(int)<<endl;

cout<<"Sizeof void* = "<<sizeof(void*)<<endl;

cout<<"Sizeof int* = "<<sizeof(int*)<<endl;

cout<<"Size of X = "<<sizeof (X)<<endl;

cout<<"Size of Y = "<<sizeof (Y)<<endl;

cout<<"Size of Z = "<<sizeof (Z)<<endl;

cout<<"Size of A = "<<sizeof (A)<<endl;

return 0;



}



I get the following output



Sizeof int = 4

Sizeof void* = 8

Sizeof int* = 8

Size of X = 4

Size of Y = 16

Size of Z = 16

Size of A = 24



Once again I am not able to understand why the sizeof Y is 16?





According to my understanding the sizeof Y would be sizeof X (4) + sizeof (virtual_table_pointer) 8 + padding required for alignment ( 0 ) = 12

But why it is 16 ? Please help me to understand this concept.











via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?31415-Question-regarding-the-size-of-the-class&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

A temporary created for the return reference from Assignement Operator?

Newsgroup: comp.lang.c++

Subject: A temporary created for the return reference from Assignement Operator?

From: pvinodhkumar@...

Date: Sat, 29 Jun 2013 04:08:00 -0700 (PDT)



class A

{

A();

A& operator = (const A& aObject_in);

int x;

int y;

};





A::A()

{

x = 0;

y = 0;

}



A& A::operator=(const A& aObject_in)

{

if(this != &aObject_in)

{

x = aObject_in.x;

y = aObject_in.y;

}



return *this; // A temporary created here?

}



int main()

{

A a1;

A a2;

a2 = a1;

}









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?31507-A-temporary-created-for-the-return-reference-from-Assignement-Operator&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Friday, June 28, 2013

2D Array

Newsgroup: comp.lang.c++

Subject: 2D Array

From: axcytz@...

Date: Fri, 28 Jun 2013 10:59:46 -0700 (PDT)



Hello all,



I have the following code to have a 2D array, and my aim is to have a matrix whose (1) column size is variable(i.e. 1st row has size 3, 2nd has 2, 3rd has 5, and so on);(2) row size is dynamically changed (I want to be able to add new rows in the future.) Unfortunately, I can't achieve these goals. Thanks for your helps in advance!



#include <string>

#include <iostream>

#include <fstream>

#include <stdio.h>



using namespace std;



int main()

{



int rowSize = 3, columnSize = 5, z=0, counter = 0;

int **array;

array = new int* [rowSize];



for (int i = 0; i < rowSize; i++)

array[i] = new int [columnSize]; // rowSize* columnSize matrix is created





int* addedRow;

addedRow = new int [rowSize]; //to add new rows







for (int z = 0; z < counter; z++ ){ //z is the index of new row

array[rowSize+z] = new int [size[z]];} //to add new row

counter++; // count the added rows

cout << "added row's number of elements:\n";

cin >> addedRow[z];



for (int i = 0; i < addedRow[z]; i++ ){

array[z][i] = 1; //all elements are set to 1

}





for(int i=0; i<rowSize; i++)

{

for(int j=0; j<n; j++)

{

cout << array[i][j] << " ";

}

cout << endl;

}



for (int i = 0; i < rowSize; i++){

delete [] array[i];

}

delete [] array;



}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?30817-2D-Array&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

function stack frames may overlap?

Newsgroup: comp.lang.c++

Subject: function stack frames may overlap?

From: Good Guy <pooria.gh@...>

Date: Fri, 28 Jun 2013 02:09:56 -0700 (PDT)



hey there

regarding following info at this address(http://en.wikipedia.org/wiki/Call_stack#Overlap):



For some purposes, the stack frame of a subroutine and that of its caller can be considered to overlap, the overlap consisting of the area where the parameters are passed from the caller to the callee. In some environments, the caller pushes each argument onto the stack, thus extending its stack frame, then invokes the callee. In other environments, the caller has a preallocated area at the top of its stack frame to hold the arguments it supplies to other subroutines it calls. This area is sometimes termed the outgoing arguments area or callout area. Under this approach, the size of the area is calculated by the compiler to be the largest needed by any called subroutine.



may a C++ compiler ever do that? I think of following case for example assuming it's not inlined and only variables in local scopes of its callers are passed to this function:



void bar(int & in){in++;}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?30495-function-stack-frames-may-overlap&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

possiblity of overlapping of function stack frames

Newsgroup: comp.lang.c++

Subject: possiblity of overlapping of function stack frames

From: Good Guy <pooria.gh@...>

Date: Fri, 28 Jun 2013 02:05:12 -0700 (PDT)







Hello

I encountered following info at this page:



For some purposes, the stack frame of a subroutine and that of its caller can be considered to overlap, the overlap consisting of the area where the parameters are passed from the caller to the callee. In some environments, the caller pushes each argument onto the stack, thus extending its stack frame, then invokes the callee. In other environments, the caller has a preallocated area at the top of its stack frame to hold the arguments it supplies to other subroutines it calls. This area is sometimes termed the outgoing arguments area or callout area. Under this approach, the size of the area is calculated by the compiler to be the largest needed by any called subroutine.



So may a C++ compiler do such thing in case of following function (assuming it's not inline of course and only variables in local scopes of callers of this function are passed as arguments to this function) or any function at all?



void foo(int & ref){ref++;}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?30494-possiblity-of-overlapping-of-function-stack-frames&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Thursday, June 27, 2013

Not able to access inherited protected member?

Newsgroup: comp.lang.c++

Subject: Not able to access inherited protected member?

From: Shriramana Sharma <samjnaa@...>

Date: Thu, 27 Jun 2013 11:10:19 -0700 (PDT)



Hello. Please consider the following minimized example for the problem I am facing.



>>>-------------------------------

template < typename T >

struct item {

typedef item<T> * pointer ;

pointer prev, next ;

} ;



template < typename ItemPtr >

struct iterator_base {

public:

iterator_base () : ptr(0) {}

protected:

ItemPtr ptr ;

explicit iterator_base ( ItemPtr p ) : ptr(p) {}

} ;



template < typename T >

struct iterator : public iterator_base < item<T> * > {

template<typename> friend class const_iterator ;

} ;



template < typename T >

struct const_iterator : public iterator_base < const item<T> * > {

const_iterator () {}

const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}

} ;



int main () {

iterator<int> it ;

const_iterator<int> cit ;

cit = it ;

}

<<<------------------------



I get the error messages:



>>>-------------------------------

$ g++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp

iterator-base-minimal-2.cpp: In constructor ?const_iterator<T>::const_iterator(const iterator<T>&)?:

iterator-base-minimal-2.cpp:28:46: error: class ?const_iterator<T>? does not have any field named ?ptr?

const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}

^

$ clang++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp

iterator-base-minimal-2.cpp:28:46: error: member initializer 'ptr' does not name a non-static data member or base class

const_iterator ( const iterator<T> & it ) : ptr(it.ptr) {}

^~~~~~~~~~~

1 error generated.

<<<-------------------------------



I am not sure why I can't access ptr which is a protected member of a parent class. OK so anyhow I thought I'll explicitly scope it by the base type and so I changed the const_iterator definition to:



>>>-------------------------------

template < typename T >

struct const_iterator : public iterator_base < const item<T> * > {

typedef iterator_base < const item<T> * > base ;

const_iterator () {}

const_iterator ( const iterator<T> & it ) : base::ptr(it.ptr) {}

} ;

<<<-------------------------------



But now I get even more cryptic error messages:



>>>-------------------------------

$ g++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp

iterator-base-minimal-2.cpp: In instantiation of ?const_iterator<T>::const_iterator(const iterator<T>&) [with T = int]?:

iterator-base-minimal-2.cpp:35:6: required from here

iterator-base-minimal-2.cpp:28:62: error: no type named ?ptr? in ?struct iterator_base<const item<int>*>?

const_iterator ( const iterator<T> & it ) : base::ptr(it.ptr) {}

^

$ clang++ -o iterator-base-minimal-2 iterator-base-minimal-2.cpp

iterator-base-minimal-2.cpp:28:52: error: typename specifier refers to non-type member 'ptr' in 'iterator_base<const item<int> *>'

const_iterator ( const iterator<T> & it ) : base::ptr(it.ptr) {}

^~~

iterator-base-minimal-2.cpp:35:8: note: in instantiation of member function 'const_iterator<int>::const_iterator' requested here

cit = it ;

^

iterator-base-minimal-2.cpp:13:10: note: referenced member 'ptr' is declared here

ItemPtr ptr ;

^

iterator-base-minimal-2.cpp:28:52: error: 'ptr' is a protected member of 'iterator_base<const item<int> *>'

const_iterator ( const iterator<T> & it ) : base::ptr(it.ptr) {}

^

iterator-base-minimal-2.cpp:13:10: note: must name member using the type of the current context 'const_iterator<int>'

ItemPtr ptr ;

^

2 errors generated.

<<<-------------------------------



I'm using GCC 4.8.1 and Clang 3.2 on Kubuntu Raring 64-bit. Can anyone please tell me what the problem is and how I can solve it? Thank you very much!







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?29996-Not-able-to-access-inherited-protected-member&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Wednesday, June 26, 2013

Linker Error: LNK2001 With This Visual Studio Project

Newsgroup: comp.lang.c++

Subject: Linker Error: LNK2001 With This Visual Studio Project

From: "clus.aol.com" <clusardi2k@...>

Date: Wed, 26 Jun 2013 12:24:24 -0700 (PDT)



//File Trains.h

#include <string>



class Trains

{

public:

static std:string haveToWash;



std::string getTrain ();

};



//File Trains.cpp

#include "StdAfx.h"

#include "Trains.h"

#include <string>



std::string Trains::getTrain()

{

return haveToWash;

}



//File with main routine

#include "StdAfs.h"

#include <string>



int main (int argc, _TCHAR* argv[])

{

return 0;

}



Build Error:

1>Trains.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Trains::havetoWash"(?haveToWash@...r_traits@...







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?29120-Linker-Error-LNK2001-With-This-Visual-Studio-Project&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Monday, June 24, 2013

Array Size

Newsgroup: comp.lang.c++

Subject: Re: Array Size

From: Juha Nieminen <nospam@...>

Date: Mon, 24 Jun 2013 07:30:47 +0000 (UTC)



Scott Lurndal <scott@...> wrote:

> And I disagree with the 'avoid unsigned types'. Use the type that's

> appropriate for the problem. The vast majority of types I use in

> my current projects are unsigned (uint32_t, uint64_t).



That principle has bitten me more than once.



For example in the past I adhered strictly to the convention of

"if negative values make no sense for an integral type, use an unsigned

type instead." Thus, for example, I used unsigned types to denote the

dimensions of an image or the screen.



And then I found myself writing things like this:



double destX = x + screenWidth / 2;



and wondering why it's not working properly.



--- news://freenews.netfront.net/ - complaints: news@... ---







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?26289-Array-Size&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Sunday, June 23, 2013

Initializing std::map Objects

Newsgroup: comp.lang.c++

Subject: Initializing std::map Objects

From: mrc2323@...

Date: Sun, 23 Jun 2013 10:57:39 -0700



I have the following structure and declarations, and I'm looking for

any simple way to initialize a set of default values.



struct TSINFO

{

string tsDescr; // T-Shirt name

string tsValue; // source T-Shirt value

char tsCode; // T-Shirt code (cShirt)

char tsGender; // T-Shirt gender version

int tsCount; // count of T-Shirts of this type

} extern tsWork;

typedef map<char, TSINFO> shirtLUMap;

shirtLUMap tsLUMap;

shirtLUMap::iterator tsluIter; // lookup iterator



That is, I wish to create, say, 5 objects with specific values and

store them into the map. For example,

tsWork.tsCode = 'S';

tsWork.tsGender = 'U';

tsWork.tsCount = 0;

tsWork.tsValue = "";

tsWork.tsDescr = "Small";

shirtLUMap['S'] = tsWork;

tsWork.tsCode = 'M';

tsWork.tsGender = 'U';

tsWork.tsCount = 0;

tsWork.tsValue = "";

tsWork.tsDescr = "Medium";

shirtLUMap['M'] = tsWork;

tsWork.tsCode = 'A';

tsWork.tsGender = 'F';

tsWork.tsCount = 0;

tsWork.tsValue = "";

tsWork.tsDescr = "Female X-Small";

shirtLUMap['A'] = tsWork;

[etc.]



Such code is tedious, and I'd like to learn some way to "shorthand"

the process of declaring certain default values. Using a non-default

constructor seems useful, but I don't know how I'd do it here... Please

advise.. TIA







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?25182-Initializing-std-map-Objects&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

[OT] Re: boost::asio, synchronous HTTP read on sslstream

Newsgroup: comp.lang.c++

Subject: [OT] Re: boost::asio, synchronous HTTP read on sslstream

From: Bart van Ingen Schenau <bart@...>

Date: Sun, 23 Jun 2013 14:33:37 +0000 (UTC)



On Sat, 22 Jun 2013 13:48:20 +0200, Torsten Mueller wrote:



> I have a problem reading on a sslstream. If the content-length is

> specified it works fine, I read exactly the given length and have a

> complet answer. If I didn't get a content-length header from the server

> I use an algorithm like this:

>

<snip>

> This should read 1k-blocks until there are no more bytes in the stream.

> The last call to the read-function should return less than 1024 bytes

> and an error code. Indeed this happens, exactly as specified, but the

> last call to the read-function doesn't return before a long timeout is

> reached, about one minute or more. What's the reason for this?

>

> I tried also other things: transfer_at_least() or transfer_all(), also

> the read_some-method of the sslstream class. All show exactly the same

> behaviour.

>

> What can I do to avoid this timeout at the end? Is this related to SSL?

> I can't remember that I had problems with this on non-SSL streams.

>

> T.M.



Your problem has nothing to do with C++ and everything with protocol

specifications and typical server behavior.

Both the SSL and the underlying TCP protocols are streaming protocols,

which means that they don't have the notion of messages or message

boundaries. If you send multiple HTTP requests over a single SSL or TCP

connection, then that is a single stream of data as far as SSL and TCP

are concerned and that stream ends when the connection gets closed.

As establishing a SSL connection (and to a lesser extent, also a TCP

connection) is a heavy weight, time consuming operation and because web

pages are typically built from different parts (which means, multiple

requests are needed), web servers will usually not close the connection

immediately after sending the first response, because it is more

economical to re-use the connection for the follow-on requests.



These two factors combined (TCP/SSL doesn't know about messages and

servers not closing the connection immediately) have the effect that if

you don't know how much data to expect, you end up waiting until the

connection gets closed due to a timeout.



Bart v Ingen Schenau







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?25046-OT-Re-boost-asio-synchronous-HTTP-read-on-sslstream&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Trying to define a generic template operator for all container classes

Newsgroup: comp.lang.c++

Subject: Trying to define a generic template operator for all container classes

From: Shriramana Sharma <samjnaa@...>

Date: Sun, 23 Jun 2013 00:21:29 -0700 (PDT)



Hello.



I am trying to define an operator<< in my program such that for any container_template_type and value_type, if con is an object of type container_template_type<value_type> and val1, val2, val3 are objects of type value_type, I can chain adding the objects to the container:



con << val1 << val2 << val3 ;



So I tried defining it as:



template < typename C, typename V >

inline C<V> &

operator << ( C<V> & con, const V & val ) { con.push_back(val) ; return con ; }



but both GCC 4.8.1 and Clang 3.2 complain about this. GCC says "error: ?C? is not a template" and Clang says "error: expected unqualified-id".



I am not sure how to tell the compiler that C is a template type. Please help.



The code:



# include <iostream>

# include <vector>



template < typename C, typename V >

inline C<V> &

operator << ( C<V> & con, const V & val ) { con.push_back(val) ; return con ; }



int main ()

{

vector<int> v ;

v << 1 << 2 << 3 ;

for ( int i = 0 ; i < v.size() ; ++i ) std::cout << v[i] << ' ' ;

std::cout << std::endl ;

}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?24761-Trying-to-define-a-generic-template-operator-for-all-container-classes&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Saturday, June 22, 2013

boost::asio, synchronous HTTP read on sslstream

Newsgroup: comp.lang.c++

Subject: boost::asio, synchronous HTTP read on sslstream

From: Torsten Mueller <irrlicht67@...>

Date: Sat, 22 Jun 2013 13:48:20 +0200



I have a problem reading on a sslstream. If the content-length is

specified it works fine, I read exactly the given length and have a

complet answer. If I didn't get a content-length header from the server

I use an algorithm like this:



asio::ssl::stream<asio::ip::tcp::socket&>* m_pSslStream;



// ... initializations ...



// ... read HTTP headers ...

// (get no content-length here)



// read the HTTP body

asio::streambuf sb;

system::error_code error;

size_t n;

while ((n = asio::read(*m_pSslStream, sb,

asio::transfer_exactly(1024), error)) > 0)

{

// ... handle contents ...

}



This should read 1k-blocks until there are no more bytes in the stream.

The last call to the read-function should return less than 1024 bytes

and an error code. Indeed this happens, exactly as specified, but the

last call to the read-function doesn't return before a long timeout is

reached, about one minute or more. What's the reason for this?



I tried also other things: transfer_at_least() or transfer_all(), also

the read_some-method of the sslstream class. All show exactly the same

behaviour.



What can I do to avoid this timeout at the end? Is this related to SSL?

I can't remember that I had problems with this on non-SSL streams.



T.M.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?23909-boost-asio-synchronous-HTTP-read-on-sslstream&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Friday, June 21, 2013

Available C++ Libraries FAQ

Newsgroup: comp.lang.c++

Subject: Re: Available C++ Libraries FAQ

From: Phlip <phlip2005@...>

Date: Fri, 21 Jun 2013 21:01:42 -0700 (PDT)



> URL: http://www.trumphurst.com/cpplibs/



> Maintainer: Nikki Locke cpplibs@...



U been at that for a decade or so, right? Time flies when you're coding...







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?23780-Available-C-Libraries-FAQ&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Thursday, June 20, 2013

Algorithms and Data structures in C++

    Newsgroups:    comp.lang.c++
    Date:    Sun, 14 Apr 2013 14:49:34 -0700 (PDT)
    Subject:    Algorithms and Data Structures in C++11
    From:    neeru.cskumar@gmail.com


1000's of computer language help forums at pocketbinaries.com.  Programming languages
                 
Generic Algorithms and Data Structures using C++11:Origin : Future of Boost C++ Libraries by Sergei Nakariakov


www.amazon.com/Generic-Algorithms-Data-Structures-using/dp/1484111540/

Book Description:

This book contains implementation of generic algorithms and data structures using C++11.

I Type Traits

1 Type Functions
2 Extended Function Traits
3 Integer Traits
4 Associated Member Types
5 Member pointers
6 Overloadable operators
7 Reference Traits
8 Type Traits
8.1 All
8.2 Assignable
8.3 Common
8.4 Convertible
8.5 Derived
8.6 Float
8.7 Function
8.8 Identity
8.9 Integer
8.10 Meta
8.11 Relational
8.12 Same
8.13 Select
8.14 Void



II Type Concepts

9 Type deduction systems
10 Overloaded Concept Implementations
11 Type Concepts
11.1 Copyable
11.2 Difference Type
11.3 Equality Comparable
11.4 Pointer Of
11.5 Reference Of
11.6 Size Type
11.7 Streamable
11.8 Totally Ordered
11.9 Value Type



III Functional Library

12 Functional Library



IV Sequence Concepts

13 Sequence Concepts Traits
14 Sequence Concepts
14.1 Iterators
14.2 Ranges
14.3 Readable and Writable
14.4 Traits
15 Range
15.1 Reference Of
15.2 Ranges
16 Range Generator
17 Sequence Algorithms
17.1 Binary Search
17.2 Copy
17.3 Count
17.4 Equal
17.5 Fill
17.6 Find
17.7 For Each
17.8 Generate
17.9 Heap
17.10Lexicographical
17.11Merge
17.12Min Max
17.13Mismatch
17.14Move
17.15Partition
17.16Permutation
17.17Quantifier
17.18Remove
17.19Replace
17.20Reverse
17.21Search
17.22Set
17.23Shuffle
17.24Sort
17.25Transform
17.26Unique
18 Iterators
18.1 Filter
19 Sequence Testing



V Memory Concepts

20 Concepts
21 Allocators



VI Matrix

22 Matrix Base
23 Slice Iterator
24 Matrix
25 Matrix Reference
26 Matrix Operations
27 Slice
28 Support Operations
29 Matrix Traits
30 Matrix
30.1 1D Matrix
30.2 2D Matrix
30.3 3D Matrix
30.4 Matrix
30.5 Matrix Operations
30.6 Slice Operations
30.7 Solver



VII Graph

31 Graph Concepts
32 Interface And Predicates
33 Graph I/O
34 Graph Handle
35 Utilities
36 Graph Edge
37 Adjacency List
37.1 Node Pool
37.2 Directed and Undirected Adjacency List
37.3 Directed and Undirected Adjacency Vector



VIII Data

38 Container Concepts
39 Optional Qualifier

Question? Why does this incorrect CRTP static_cast compile?

Newsgroups:    comp.lang.c++
    Date:    Wed, 24 Apr 2013 20:18:27 -0700 (PDT)
    Subject:    Why does this incorrect CRTP static_cast compile?
    From:    kfrank29.c@gmail.com

See all of the newgroup complang groups at pocketbinaries.com.

Hello Group!

The Wikipedia CRTP article:

   http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

has a comment that confused me:

   Pitfalls

   One issue with CRTP is that the correct usage of the
   pattern by derived classes cannot be checked at compile
   time. For example, with the above example of Shape_CRTP,
   if the definition of Circle were changed to:

      class Circle: public Shape_CRTP<Square> {};

   it would still compile with no errors. However, running
   code that performs clone() on a Circle object will lead
   to undefined behavior.

(By the way, Mark's thread, "enforce access to derived class
via pointer to base," got me thinking about CRTP and led me
to the Wikipedia article.)

I've analyzed why I think the Wikipedia example should compile,
but I'm really not sure I understand what's going on.

(For context and details, see the article.)

I've put together a stripped-down example that illustrates
the main points of my analysis.

The core issue is that code within the template CRTP base
class looks as if it's making a compile-time illegal
static_cast when instantiated incorrectly, and I'm trying
to figure out why it's compile-time legal.

(I am a little foggy on what, exactly, static_cast is and
in not allowed to do, and I find the language in the
standard difficult to follow.)

Here is my example, followed by my analysis:


==========


class B {   // base class for example
};

template <typename T> class CR : public B {   // template class for CRTP
  public:
    void f() {   // doesn't matter if this is virtual
      // why does this compile when instantiated with F?
      T *pt = static_cast<T*>(this);
    }
};

class E : public CR<E> {   // normal CRTP idiom
};

class F : public CR<E> {   // error -- should derive from CR<F>
};

class X : public B {   // to illustrate bad static_cast without CRTP
};

class Y : public B {   // to illustrate bad static_cast without CRTP
};

int main (int argc, char *argv[]) {
  E e;
  e.f();   // okay -- static cast from base* to derived*

  F f;
  f.f();   // why doesn't this cause bad static_cast to be instantiated?

  X x;

  // error: invalid static_cast from type 'X*' to type 'Y*'
  // Y *py = static_cast<Y*>(&x);

  // compile-time legal static_cast from X* to B* to Y*
  Y *py = static_cast<Y*>(static_cast<B*>(&x));
}


==========


(Note, CR derives from B only to mirror more closely the
Wikipedia example.  I have the same analysis and confusion
without it.)

I think the point is that whether or not the function
CR<T>::f is virtual, the compile-time static type of
this in the code for f is CR<T>* for whatever T is when
CR is instantiated, even though at run time the type of
this is actually E* or F* (classes derived from CR).

The line e.f() causes the member function CR<E>::f() to be
instantiated.  This causes the static cast from type CR<E>*
(the static type of this) to E* to be instantiated.  This
should be fine, and is part of the standard CRTP idiom.

If the static type of this were E* for E derived from
CR<E>, then we would not even need the static cast, but
we do.

The line f.f() causes CR<F>::f() to be instantiated.
The way I analyze this, we still have a static_cast from
CR<E>* (still the static type of this) to E* -- still okay,
because E derives from CR<E> (even though we are dealing
with an F).

For an F, the dynamic type of this is F*.  If the static
type of this were also F* for F derived from CR<E>, then
the static_cast wouldn't be enough to get from F* to E*.

I've tried to draw an analogy with using static_cast with
the complication of the CRTP stripped away.

At the end of the example code, X and Y both derive from
B.  We can't static_cast from X* to Y* because neither does
X derive from Y nor vice versa.  But we can do a two-step
static_cast from X* to B* to Y* (legal at compile time,
but undefined behavior at run time), because X and Y
derive from a common base class.

I believe that this is analogous to instantiating F::f().
this for F is of type F*, but the static type of this in
the code for f in CR<T> (when instantiated as CR<E>) is
CR<E>*.  This is analogous to a static_cast of F* to its
pointer-to-base, CR<E>*.  We then static_cast to E*, which
is compile-time legal (but undefined behavior at run time)
because E also derives from CR<E>.

To belabor the point, the two-step conversion:

   F* --> CR<E>* --> E*

is analogous to the previous:

   X* --> B* --> Y*

because E and F have CR<E> as their common base class,
and conversion from F* to E* is compile-time legal /
run-time undefined for the same reason that X* --> Y*
is.

There are a lot of steps in this chain of reasoning, and
I'm uncertain of the details.  Any corrections or
clarifications would be very welcome.

Compilation of awkward syntax

Newsgroups:    comp.lang.c++
    Date:    Sat, 27 Apr 2013 05:58:11 -0700 (PDT)
    Subject:    Compilation of Awkward Syntax
    From:    Joe Snodgrass <vaughan.andursen@.com>


View all of the computer language help forums at pocketbinaries.com.

I've almost finished teaching myself C++, but there's one last step I
have to take.  I need to teach myself all the situations that require
one to use a strange looking combination of operators.

One example of a strange looking combination of operators that I
managed to work through is

tmp=(Node<int>*)ptr,

You can see that the >* is nothing like anything that would have been
seen in any previous (or structured) language.

I seem to recall seeing this sort of awkward syntax in several
different situations, when I read 2nd edition of Stroustrup, not just
this situation.

Does anybody know if someone has assembled a cheat sheet to pull them
all together in one place?  TIA.

How to get dbus Running


Read all the programming help forums at: complang help

From:    "Daniel47@teranews.com" <dxmm@albury.nospam.net.au>
    Newsgroups:    alt.os.linux
    Subject:    How to get dbus running??
    Date:    Sat, 11 Aug 2012 20:33:35 +1000

Several times, over the years, I've tried to do something in Mandriva 2009.0 that would require me to use e.g. Dolphin, Konqueror or Kwrite as root and all that would happen is that I would end up with a bash screen informing me

quote

[root@localhost daniel]# kdeinit4: preparing to launch /usr/lib64/kde4/libexec/klauncher
klauncher(20004) kdemain: No DBUS session-bus found. Check if you have started the DBUS server.
kdeinit4: Communication error with launcher. Exiting!
konqueror(20000): No ksycoca4 database available!

konqueror(20000): No ksycoca4 database available!

end quote

and then, as well, a warning screen saying, e.g.:-

quote
Could not delete file file:///root/.kde4/share/apps/konqueror/autosave/owned_by.
end quote

and then, when I'm closing down for the night, I finally get my (in this case) Konqueror screen.

A Dolphin search, as user not root, shows me I have eleven "dbus" folders, but no "dbus" program file, not even in the four "DBus" (note the capitals) folders that seem to have something to do with "perl5".

Even under my MD2010.1 installation, I seem to have no dbus either!!

Anybody got any suggestions as to how I might fix my problems??

TIA

Daniel