Thursday, February 27, 2014

calling placement new for elements of a deque

Newsgroup: comp.lang.c++

Subject: calling placement new for elements of a deque

From: hbdevelop1@...

Date: Wed, 26 Feb 2014 21:50:29 -0800 (PST)



Hello

For optimisation reasons, I am re-using not used objects in my deque for new objects.

I am doing that this way :

void Score::Add(const char *str, Coordinates &sp)

{

for(deque<Text>::iterator it=scores.begin();it!=scores.end(); ++it)

{

if(it->notUsed==1)

{

it->Reset(str,sp);

return;

}

}



scores.push_back(Text(str,sp));

}



I thought it would be more convenient to call the placement new on the notUsed object instead of Reset.

How can I do this ?

How can I get the address of the not used object so I can use it in the new placement operator?





Thank you in advance







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1cWtAxS

View all the progranning help forums at:

http://ift.tt/1dP9txN

How to download Microsoft Visuail c++ software

Newsgroup: comp.lang.c++

Subject: How to download Microsoft Visuail c++ software

From: 893756098a@...

Date: Tue, 25 Feb 2014 20:22:02 -0800 (PST)



Recently,our class had studied Microsoft Visuail c++.So I want to practice after school,but I can't install this.I would appreciate it if someone could do me a favour!







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1cWtAxC

View all the progranning help forums at:

http://ift.tt/1dP9txN

Sunday, February 23, 2014

hw to get started

Newsgroup: comp.lang.c++

Subject: hw to get started

From: erson212 <erson212@...>

Date: Sun, 23 Feb 2014 01:20:06 -0800 (PST)



Please can some 1 tell me hw to get started with c++, first tin to du







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/MlsTUe

View all the progranning help forums at:

http://ift.tt/1dP9txN

shared_ptr and unique_ptr related question

Newsgroup: comp.lang.c++

Subject: shared_ptr and unique_ptr related question

From: somenath <somenathpal@...>

Date: Fri, 21 Feb 2014 17:28:19 -0800 (PST)



To get some understanding of shared_ptr and unique_ptr I wrote the following naive implementation of linked list.



#include<memory>

using namespace std;



template <class T>

class List {

private:

class ListItem {

public:

ListItem( T Val );

shared_ptr <ListItem > Next;

T Data;



};



shared_ptr< ListItem >Head;



public:

int CreateNode();

List() {

Head.reset();

}

void PushBack(T Val);

void Dump();

};





template<class T>

List<T>::ListItem::ListItem( T Val):Data(Val) {

Next.reset();





}

template<class T>

void List<T>::PushBack( T val)

{

unique_ptr<ListItem > NewItem (new ListItem(val));

if (!Head ) {

Head = move(NewItem);

}

else {

shared_ptr<ListItem> Curr(Head);

shared_ptr<ListItem> Prev(Head);

while( (Curr) ) {

Prev = Curr;

Curr = Curr->Next;

}

Prev->Next = move(NewItem);

}

}



template<class T>

void List<T>::Dump() {

shared_ptr<ListItem > Curr(Head);

while ( Curr) {

cout<<"Val = "<<Curr->Data<<endl;;

Curr = Curr->Next;

}

}



But I am not very clear of the correct uses of shared_ptr and auto_ptr yet. Could you please comment on the uses of smart pointers in the context of my linked list code. According to my understanding where I do not need to assign smart pointers to other one I use unique_ptr . Is this understanding correct?



Also I am not able to get convinced the benefit of shared_ptr and unique_ptr in the context of my code. I could have used even raw pointers to implement the same without losing much benefit . Is it not the case here?



I can use the above code as



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

{

unique_ptr< List <int> > ilst (new List<int>());



ilst->PushBack(5);

ilst->PushBack(15);

ilst->PushBack(25);

ilst->Dump();



return 0;

}



Is the benefit of using smart pointers is, ilst need to be freed manually?



Please point me to some real code where smart pointers has been heavily used.



--

Somenath







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1pdpjLy

View all the progranning help forums at:

http://ift.tt/1dP9txN

Friday, February 21, 2014

implicit passing by reference

Newsgroup: comp.lang.c++

Subject: implicit passing by reference

From: jerry.jeremiah@...

Date: Thu, 20 Feb 2014 20:45:27 -0800 (PST)



In this topic



http://ift.tt/1fobfKU



The person that answers it says:



You have to remember that objects are sent to functions 'by reference'.



But the object being passed contains only one int member and the function the object is being passed to clearly takes it's parameter by value and stores it in a member value.



Do objects really always pass by reference regardless of what the parameter list says? And, if so, why would it do that? I would usderstand if the function took its parameter explicitly by reference...



Thanks for clearing up mu confusion.



Jerry







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1dcvLKj

View all the progranning help forums at:

http://ift.tt/1dP9txN

Thursday, February 20, 2014

Working with Large Values (double)

Newsgroup: comp.lang.c++

Subject: Working with Large Values (double)

From: mrc2323@...

Date: Thu, 20 Feb 2014 17:25:50 -0700



How can I express a large value as a double? (e.g. 1000000.3)

Whereas this compiles and executes, when I try to convert it to a

string value it converts as a scientific string (e.g. "1e+006", not

"1000000.3"). I want to process all the characters of the value of the

data as a std::string.

Or is there a way to convert the double to assure it's not expressed

in scientific notation? TIA



---

This email is free from viruses and malware because avast! Antivirus protection is active.

http://www.avast.com









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1gOIwiN

View all the progranning help forums at:

http://ift.tt/1dP9txN

OT: Problem building libc++

Newsgroup: comp.lang.c++

Subject: OT: Problem building libc++

From: woodbrian77@...

Date: Thu, 20 Feb 2014 15:00:24 -0800 (PST)





I was following these instructions



http://ift.tt/UxFMKX



but have a problem when I get to building libc++.



Linking CXX shared library libc++.so

/usr/bin/ld: cannot find -lsupc++





I tried:



find / -type f -name "*supc++*" -print



but it didn't find any files that matched that.



I searched on https://duckduckgo.com and bing for

some ideas, but haven't found the answer.



What do you suggest? Thanks in advance.



Brian

Ebenezer Enterprises - In G-d we trust.

http://webEbenezer.net







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1hzSmrQ

View all the progranning help forums at:

http://ift.tt/1dP9txN

Nested Boost::unordered_map with std::pair insertion help ..

Newsgroup: comp.lang.c++

Subject: Nested Boost::unordered_map with std::pair insertion help ..

From: Rahul Mathur <srivmuk@...>

Date: Thu, 20 Feb 2014 04:32:58 -0800 (PST)



All,



I have having Boost boost::unordered_map nested map calling boost::unordered_map which calls std::map finally.



The LHS (towards RHS) side key for first boost::unordered_map is 'int' type followed by next key being char buffer value of size 10, and key for std::map being again a char buffer value of size 10. So the boost::unordered_map has nested map internally.

I have to insert the char buffer of size 10 (value being either 'ORANGE' or ORD123 as test case) into nested std::pair as defined below. After inserting, I have to search or find the value from char buffer of size 10.



The code as compiled on Linux is -



----

#include <cstdio>

#include <cstdlib>

#include <iostream>

#include <boost/unordered_map.hpp>

#include <map>

#include <algorithm>

#include <string>



using namespace std;



#if 0

#pragma pack(push, 2)

struct BookKeeping {

int ABCD;

char Apple_ID [10];

char Apple_NAME[10];

int Price;

int Number;

};

#pragma pack(pop)

#endif



class Name { // buffer of size 10

char str[10];

public:

Name() {

strcpy(str, "");

}

Name(char *s) {

strcpy(str, s);

}

char *get() {

return str;

}

};



bool operator<(Name a, Name b)

{

return strcmp(a.get(), b.get()) < 0;

}



#pragma pack(push, 2)

struct BookKeeping {

int ABCD;

Name Apple_ID;

Name Apple_NAME;

int Price;

int Number;

};

#pragma pack(pop)



BookKeeping *_bkkping;



typedef boost::unordered_map< int, boost::unordered_map< Name, std::map< Name, int > > > _uordmap; // nested boost::unordered_map



class BkKping {

private:

_uordmap * uordmap;



typedef boost::unordered_map< int, boost::unordered_map< Name, std::map< Name, int > > >::iterator _itr_1;

typedef boost::unordered_map< Name, std::map< Name, int > >::iterator _itr_2;

typedef std::map< Name, int >::iterator _itr_3;



public:

BkKping () {

uordmap = new _uordmap ();

}

void Apple ( int abcd, const char * Apple_ID_, const char * Color_, int Price_ );

~BkKping () {

delete uordmap;

}



};



void BkKping::Apple ( int abcd, const char * Apple_ID_, const char * Color_, int Price_ ) {



uordmap->insert( std::pair< int, std::pair< Name, std::pair< Name, BookKeeping &> > > ( _bkkping->ABCD , _bkkping->Apple_ID, _bkkping->Apple_NAME, *_bkkping) ); // nested std::pair<> insertion

}



int main () {



int abcd;

const char * Apple_ID_;

const char * Color_;

int Price_;



BkKping book;

book.Apple( abcd, Apple_ID_, Color_, Price_);



}

-----



The error message is -



---

hello.cpp: In member function âvoid BkKping::Apple(int, const char*, const char*, int)â:

hello.cpp:76: error: no matching function for call to âstd::pair<int, std::pair<Name, std::pair<Name, BookKeeping&> > >::pair(int&, Name&, Name&, BookKeeping&)â

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:83: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = std::pair<Name, std::pair<Name, BookKeeping&> >]

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:79: note: std::pair<_T1, _T2>::pair() [with _T1 = int, _T2 = std::pair<Name, std::pair<Name, BookKeeping&> >]

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_pair.h:68: note: std::pair<int, std::pair<Name, std::pair<Name, BookKeeping&> > >::pair(const std::pair<int, std::pair<Name, std::pair<Name, BookKeeping&> > >&)

--



I have created a class buffer to read size 10.



Please help.



P.S: No issue with Boost compilation, simply looking for proper semantic call for nested std::pair<>.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1hzSmbg

View all the progranning help forums at:

http://ift.tt/1dP9txN

Wednesday, February 19, 2014

Iterator pair as a function argument?

Newsgroup: comp.lang.c++

Subject: Iterator pair as a function argument?

From: nvangogh <nvangogh@...>

Date: Wed, 19 Feb 2014 20:22:47 +0000



Suppose I have a vector<int> values. Now the pair values.begin() and

values.end() will cover the range of the vector.



I am writing a simple function that takes the pair of iterators as

arguments along with an int value. The function then returns a bool to

indicate if the int value is found in the vector.



The first problem I have is to question if it is possible to pass

iterators as distinct arguments to a function? I would have thought that

the only way to accomplish this would be to pass the vector as a

reference, from there the function can use the iterators to do it's work.



I was thinking the correct definition would be



bool find_value (vector<int>&, int);



Or is there a way to pass an iterator pair as function arguments which I

have missed?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1jJceFN

View all the progranning help forums at:

http://ift.tt/1dP9txN

could an braced-init-list be a first-class expression?

Newsgroup: comp.lang.c++

Subject: could an braced-init-list be a first-class expression?

From: pietro.cerutti@...

Date: Wed, 19 Feb 2014 10:47:33 -0800 (PST)



I'm wondering what would prevent an initialization list from being a first-class expression of type std::initializer_list.



I understand that the current standar allows braced-init-lists to appear only when assigning to a scalar of type T or to an object of a class taking an std::initializer_list in the assignment operator.



Having



void f (std::vector<double> v);



It is currently possible to do this:



auto a {1, 2, 3}:

g (a);



but not to do this:



g ({1, 2, 3});



As I see it, all information the compiler needs is there. Is there any specific reason why this isn't possible?



Thanks,







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/MC6McR

View all the progranning help forums at:

http://ift.tt/1dP9txN

IO problem

Newsgroup: comp.lang.c++

Subject: IO problem

From: nvangogh <nvangogh@...>

Date: Wed, 19 Feb 2014 14:29:18 +0000



I have come to a question in C++ Primer (p 314 , exercise 8.1) that is

not clear to me.

"Write a function that takes and returns an istream&. The function

should read the stream until it hits end-of-file. The function should

print what it reads to the standard output. Reset the stream so that it

is valid before returning the stream."



Breaking this down, the function has to do three things:



1. Read a stream until it hits end-of-file

So the >> operator reads input from an istream object - cin.

This stream's end of file can be interrogated by

cin.eof(). This returns true if the end of file bit is set which can be

tested with a bool variable

bool on = false;

on = cin.eof();

if(on == true)

// end of file is reached, else

if(on ==false)

// keep reading cin



I don't believe that this is completely correct so can someone show me

how this code should be presented?



2. Print what is read to the standard output

I can only imagine this to be cout << ? But am lost from here



3. Reset the stream so it is valid before returning the stream

This section of the problem again defeats me.



Can anyone help with this function?









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/MC6KSh

View all the progranning help forums at:

http://ift.tt/1dP9txN

pointer to a vector

Newsgroup: comp.lang.c++

Subject: pointer to a vector

From: "A" <a@...>

Date: Wed, 12 Feb 2014 20:46:12 +0100



There are 2 vectors each one having a structure



struct MyStruct

{

int a;

std::string b;

}



std::vector<MyStruct> v1;

std::vector<MyStruct> v2;



Now I want a pointer to which one I will use... v1 or v2



std::vector<MyStruct> *v = (condition)? &v1 : &v2;



Finally I access it using:



for (unsigned i = 0; i < v->size(); i++)

{

v->operator[](i).a = i + 1;

}



My lack of understanding here is:



a) does the above *v needs to be deleted? Isn't it just a pointer variable?

Or it works differently when it points to a vector? Just to be clear, I

don't actually need to delete v1 or v2. I just need to cleanup *v if

required.



b) what's the heap or stack or difference or advantage of them in relation

to the above?











via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/MC6KBR

View all the progranning help forums at:

http://ift.tt/1dP9txN

about gets

Newsgroup: comp.lang.c++

Subject: about gets

From: mary8shtr@...

Date: Tue, 11 Feb 2014 09:22:12 -0800 (PST)



hi.I am writing a program.while i define some variable but program say this variable undefined.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/MC6KBH

View all the progranning help forums at:

http://ift.tt/1dP9txN

Tuesday, February 18, 2014

Flummoxed - Please Help!

Newsgroup: comp.lang.c++

Subject: Flummoxed - Please Help!

From: mrc2323@...

Date: Tue, 18 Feb 2014 12:40:26 -0700



I have the following (rather simple, I think) code which compiles but

executes in a bizarre way: the code in the subprogram is skipped when

called. 8<{{

Here's the code and the call to it I use:



string spellNumber(double value)

{

bool showThousands = false;

bool allZeros = true;

int ii, dPos, nn;

char wc;

string digits, temp;

ostringstream ossw;

static string builder;

ossw.str("");

ossw << value;

digits = ossw.str();

dPos = digits.find('.');

if(dPos != string::npos) digits.erase(dPos);

nn = digits.length();

wc = digits.back();

for(ii = digits.length()-1; ii >= 0; ii--)

{

int ndigit = (int)(digits[ii]-'0');

int column = (digits.length()-(ii+1));

} // for

// more code to be added here

return builder;

}

...

spellNumber(123.45); // call the function



I have, of course the normal stdafx.h header and "use namespace

std;", and this is code from a small test program I use to debug new

functions and such. Many other programs and many thousands of lines of

code work fine, but this code is weird...and I can't see what's wrong

with it!

Please advise... TIA



---

This email is free from viruses and malware because avast! Antivirus protection is active.

http://www.avast.com









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/O7tKtA

View all the progranning help forums at:

http://ift.tt/1dP9txN

SMITHSONIAN SHUT DOWN FOR GOOD -- THE THRINAXODON TIMES REPORTS, YOU CALL OUT BULLSHIT

Newsgroup: comp.lang.c++

Subject: SMITHSONIAN SHUT DOWN FOR GOOD -- THE THRINAXODON TIMES REPORTS, YOU CALL OUT BULLSHIT

From: Apidium23 <apidium23@...>

Date: Tue, 18 Feb 2014 14:05:26 -0500



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

>BREAKING NEWS

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

>

SMITHSONIAN FINALLY SHUT DOWN AFTER YEARS OF CENSORSHIP, SCAMS AND CON

ARTISTRY.

>

THRINAXODON BLEW DOWN THE BUILDINGS, LIT IT ON FIRE AND HAD THE ASSHOLES

ARRESTED.

>

R. DAWKINS WAS THROWN IN THE DOGHOUSE, ONLY TO GET KILLED BY ANGRY

FELONS WHO WANTED PAYBACK FOR FRAMING THEM.

>

THRINAXODON DANCED ON DAWKINS' GRAVE, AND BURNED A FEW SMITHSONIAN

MAGAZINES.

>

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

EVIDENCE THAT HUMANS LIVED IN THE DEVONIAN:



http://ift.tt/O7tKcQ



http://ift.tt/1cXKQzc



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



http://ift.tt/O7tKcU



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



THRINAXODON ONLY HAD THIS TO SAY:



"I..I...I...Can't believe it. This completely disproved Darwinian

orthodoxy."



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



THE BASTARDS AT THE SMITHSONIAN, AND THE LEAKEY FOUNDATION ARE ERODING

WITH FEAR.



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

THESE ASSHOLES ARE GOING TO DIE:

THOMAS AQUINAS;

ALDOUS HUXLEY;

BOB CASANVOVA;

SkyEyes;

DAVID IAIN GRIEG;

MARK ISAAK;

JOHN HARSHAM;

RICHARD NORMAN;

DR. DOOLITTLE;

CHARLES DARWIN;

MARK HORTON;

ERIK SIMPSON;

HYPATIAB7;

PAUL J. GANS;

JILLERY;

WIKI TRIK;

THRINAXODON;

PETER NYIKOS;

RON OKIMOTO;

JOHN S. WILKINS

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



THRINAXODON WAS SCOURING ANOTHER DEVONIAN FOSSIL BED, AND FOUND A

HUMAN SKULL, AND A HUMAN FEMUR. HE ANALYSED THE FINDS, AND SAW THAT

THEY WERE NOT NORMAL ROCKS. THESE WERE FOSSILIZED BONES. THEY EVEN HAD

TOOTH MARKS ON THEM. SO, THRINAXODON BROUGHT THEM TO THE LEAKEY

FOUNDATION, THEY UTTERLY DISMISSED IT, AND SAID, "We want to keep

people thinking that humans evolved 2 Ma." THRINAXODON BROUGHT HIS

SWORD, AND SAID, "SCIENCE CORRECTS ITSELF." RICHARD LEAKEY SAID, "That

is a myth, for people to believe in science." THRINAXODON PLANS TO

BRING DOOM TO SCIENCE, ITSELF.



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



THRINAXODON IS NOW ON TWITTER.



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

>

THRINAXODON WAS AWARDED

US$100,000,000,000,000,000,000,000,000,000,000,000 ,000,000,000,000,000,000

DOLLARS FOR HIS BRAVE EFFORTS IN SHUTTING DOWN THE EVOLUTIONARY SCAMS.

--

Apidium23:



http://ift.tt/1cXKPLR









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/O7tKcW

View all the progranning help forums at:

http://ift.tt/1dP9txN

Saturday, February 15, 2014

Can't think of a good subject

Newsgroup: comp.lang.c++

Subject: Can't think of a good subject

From: woodbrian77@...

Date: Wed, 12 Feb 2014 19:45:10 -0800 (PST)





I'm not sure why the behavior of the following

two functions differs here. The only difference

in these functions is in the body of the loop.



template <class R>

void Receive (::cmw::ReceiveBuffer<R>& buf

,empty_container<cmw::File>& az1)

{

int32_t count[1];

count[0]=buf.template Give<uint32_t>();

for(;count[0]>0;--count[0]){

cmw::File inst(buf); // named

}

}





template <class R>

void Receive (::cmw::ReceiveBuffer<R>& buf

,empty_container<cmw::File>& az1)

{

int32_t count[1];

count[0]=buf.template Give<uint32_t>();

for(;count[0]>0;--count[0]){

cmw::File (buf);

}

}





The first (named) version works the way I want

it to, and the second version, built with either

gcc or clang doesn't. Both compilers are

producing smaller text segments for the second

version. And the results for that version

(both compilers) are like nothing is being done

in the loop.



Can someone explain this? In another file I

have some code similar to the second version

and it works fine:



while(fgets(lineBuf,300,Fl.FlHndl)){

token=strtok(lineBuf," ");

if(strcmp("Header",token)) break;

cmw::File (strtok(nullptr,"\n ")).Marshal(buf);

}



I've simplified that loop a little, but don't

think I've removed anything important. There

are different constructors being used in these

two cases.



http://ift.tt/1kJmjpJ



Thanks.



Brian

Ebenezer Enterprises - In G-d we trust.

http://webEbenezer.net







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1gDONNy

View all the progranning help forums at:

http://ift.tt/1dP9txN

Friday, February 14, 2014

a totally self balanced tree for unsigned integers...

Newsgroup: comp.lang.c++

Subject: a totally self balanced tree for unsigned integers...

From: "Chris M. Thomasson" <no@...>

Date: Fri, 14 Feb 2014 13:44:40 -0800



I do not even want to go into why James tried to post this as his own!



:^/ GRRRR!!!!! #$%#$% FUC32432KL#$@...





Anyway, He made a fatal mistake! My name is in my cryptic MACROS!!!!



Take a look:



http://ift.tt/1eYaCcl





HAW HAW!!!!!





:^)





Anyway, I need to use it now for a database.







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1eYaEB1

View all the progranning help forums at:

http://ift.tt/1dP9txN

Sunday, February 9, 2014

Return by reference

Newsgroup: comp.lang.c++

Subject: Return by reference

From: Giuliano Bertoletti <gbe32241@...>

Date: Sun, 09 Feb 2014 12:50:38 +0100





Hello,



I've a classes like this:



class SubObject { ... };



class Object {

public:

SubObject sub;

};



class MyClass {

public:

Object obj;



public:

SubObject &GetSubObject() { return obj.sub; } // shortcut

};



which is the difference of calling?



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

MyClass c;



SubObject &sub = c.GetSubObject();

SubObject sub = c.GetSubObject();

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



It compiles both ways.



In practice I've an higher number of nested classes, so GetSubObject()

is actually a shortcut which digs deep into MyClass and retrieves the

item I need.



Giulio.























via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1fWFLLL

View all the progranning help forums at:

http://ift.tt/1dP9txN

Thursday, February 6, 2014

show all subset of a set

Newsgroup: comp.lang.c++

Subject: show all subset of a set

From: mary8shtr@...

Date: Thu, 6 Feb 2014 08:13:54 -0800 (PST)



A program is a set of all subsets of this part of the show. Users can enter a number., For example, if n = 2 the output looks like this:

{}

{1}

{2}

{1,2}

or for n=3 we have:

{}

{1}

{2}

{3}

{1,2}

{1,3}

{2,3}

{1,2,3}

meantime this program should be solved with tow way.

recursive function and Non-recursive.Can also be used in solving the problem of bitwise operations.



my algorithm that is for per subset exists one number. these numbers are form 0 until (2^n)-1.allow me explain it with a example.

n=3

{ } 000 0

{1} 100 4

{2} 010 2

{3} 001 1

{1,2} 110 6

{1,3} 101 5

{2,3} 011 3

{1,2,3} 111 7

The third column show some number(fn) that i use in code.now we know per number of subset has to state(sn).0 or 1. this means it exists or not exists.now i should match fn and sn.and suggested way is use of bitwise operators.(operator &).and now i don't know what do and thing with else thing.

#include <iostream>

using namespace std;

#include <conio.h>

#include <string.h>

int pow(int a,int b)

{

int p=1;

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

p=p*a;

return p;

}

struct ar

{

int binary;

int content;

};

int main()



{

int testcase=0;

int n;

ar *a;

a=new ar [2000];

cin>>testcase;

int i=0;

while(i<testcase)

{

cin>>n;

for(int j=0,k=1;j<n;j++,k++)

{

a[j].content=k;

a[j].binary=2;

}





for(int p=0;p<pow(2,n)-1;p++)

{

cout<<'{';

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

{



int b;

b=a[m].binary&p;

if(b==1)

cout<<a[i].content<<' ';



}

cout<<'}';

}



cout<<endl;

i++;

}

return 0;

}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/LVdQRq

View all the progranning help forums at:

http://ift.tt/1dP9txN

Wednesday, February 5, 2014

question regarding the shared_ptr use_count

Newsgroup: comp.lang.c++

Subject: question regarding the shared_ptr use_count

From: somenath <somenathpal@...>

Date: Sun, 2 Feb 2014 17:12:57 -0800 (PST)



I am not able to understand the behavior of the following program



#include <iostream>

#include<memory>

#include<string>

using namespace std;



int main() {

auto p1 = make_shared<string> (10,'S');

cout<<"p1 use_count = "<<p1.use_count()<<endl;

shared_ptr<string> p2(new string()) ;

p1 = p2;

cout<<"p2 use_count = "<<p2.use_count()<<endl;

cout<<"second p1 use_count = "<<p1.use_count()<<endl;

return 0;

}

Output

++++++++++++++++

p1 use_count = 1

p2 use_count = 2

second p1 use_count = 2



I can understand the first two print. At beginning p1 points to one string so the reference count is 1.

When the statement p1=p2; executes p2's reference count gets incremented to 2 but at the same time I was expecting that p1's reference count to be decremented by 1 as p1 would be pointing to p2 now.

Please let me know where my understanding is going wrong?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1keZz0q

View all the progranning help forums at:

http://ift.tt/1dP9txN

Saturday, February 1, 2014

Address one past the end of array - is this syntax a valid C++?

Newsgroup: comp.lang.c++

Subject: Address one past the end of array - is this syntax a valid C++?

From: Peter <pilarp@...>

Date: Sat, 1 Feb 2014 09:23:39 -0800 (PST)



Assume we have an array:



int arr[5];



It's legal to refer to address arr + 5, but, of course, illegal to refer to element arr[5] as it's not part of the array. However, arr + n is equivalent to &arr[n]. My question is: does this equivalence also hold for an edge case of

n = 5 (or, generally, n equal to number of elements of array)?



While there's nothing wrong with arr + 5, &arr[5] looks highly suspicious: it looks like in the first step arr[5] is evaluated (which introduces an undefined behaviour) which would mean the expression as a whole is undefined. Does the equivalence still hold in this special case?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1i4Tpfr

View all the progranning help forums at:

http://ift.tt/1dP9txN