Friday, September 22, 2017

Digest for comp.lang.c++@googlegroups.com - 12 updates in 4 topics

Mwater07 <mwater07@gmail.com>: Sep 21 06:11PM -0700

I would like to write a program that stores nonzero elements of 2 different vectors into 2 different linked lists with their indexes. Then would like t to calculate the dot product and displays the result. I am struggling to read in vector values with indexes into a link list. Please advice
 
#include <iostream>
#include <vector>
using namespace std;

struct node{
int index; /* original index of non-zero array element */
int value ; /* integer non-zero value at index x */
node *next;
};

class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}

void create (int x[], int n)
{
link head=0;
for(int i=0; i<n;i++)
head=new node (a[i],head);
}

 
};

 
vector<int> x={0,0,7,0,5,0,0,8,0,4};
vector<int> y={0,0,0,5,6,0,0,0,0,5};

 
int dotproduct() /*Computes the stored nonzero values and returns the result to product*/
{
int product=0;
while(A!=0 && B!=0)
{
if(A->index == B->index)
{
product = product + A->value * B->value;
A=A->next;
B=B->next;

}
else if(A->index < B->index)
{
A=A->next;
}
else
{
B=B->next;
}
}
return product;
}

int main()
{
generate_A_and_B() ;
int r;
r=dotproduct();
cout<<"Dot Product = "<<r<<endl;
return 0;
}
Barry Schwarz <schwarzb@dqel.com>: Sep 21 09:33PM -0700

On Thu, 21 Sep 2017 18:11:43 -0700 (PDT), Mwater07
 
>I would like to write a program that stores nonzero elements of 2 different vectors into 2 different linked lists with their indexes. Then would like t to calculate the dot product and displays the result. I am struggling to read in vector values with indexes into a link list. Please advice
 
What part are you having trouble with?
You can scan a vector with a simple for loop
for (i = 0; i < vector.size; i++)
{do something with v[i]}
There is a lot of code available showing how to add elements to a
linked list. Google can find examples for you.
 
> product = product + A->value * B->value;
> A=A->next;
> B=B->next;
 
Your use of expressions like A-<value and B->next tell us that A and B
have type node*. If you are going to write code, you should include
the declarations and the code to initialize them. Based on the
comments below, you could changed the signature to
int dotproduct(linked_list *p1, linked_list *p2)
and then populate A and B with code similar to
node* A = p1->head;
after making head public, or create a member function called get_head
and use
mode *A = p1->get_head();
 
 
>int main()
>{
> generate_A_and_B() ;
 
Rather than one function to generate both linked lists, I think you
would be better off with
link_list *link_x = new linked_list;
link_list *link_y = new linked_list;
x->head = generate_list(x);
y->head = generate_list(y);
 
> int r;
> r=dotproduct();
 
And then call dotproduct with
r =dotproduct(link_x, link_y);
 
 
--
Remove del for email
Jorgen Grahn <grahn+nntp@snipabacken.se>: Sep 22 08:17AM

On Fri, 2017-09-22, Mwater07 wrote:
 
> indexes. Then would like t to calculate the dot product and displays
> the result. I am struggling to read in vector values with indexes
> into a link list. Please advice
...
> {
> private:
> node *head,*tail;
...
 
Is this a school exercise? It's almost always a bad idea to create
your own linked lists instead of using std::list (and using std::list
is usually a bad idea, too).
 
Not wanting to invent this particular wheel is one of the main reasons
I write in C++ instead of C.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Sep 22 11:19PM +0100

On 22/09/2017 09:17, Jorgen Grahn wrote:
[snip]
> Is this a school exercise? It's almost always a bad idea to create
> your own linked lists instead of using std::list (and using std::list
> is usually a bad idea, too).
 
Using std::list is not usually a bad idea if you usually use std::list
when appropriate to do so.
 
/Flibble
woodbrian77@gmail.com: Sep 22 08:00AM -0700

> https://groups.google.com/forum/#!topic/comp.lang.c++/OB5cGxihXa4
 
> Would someone mind checking this again?
> https://github.com/Ebenezer-group/onwards
 
I was able to test this now on FreeBSD with clang 5.0.0
and the code compiled fine. I had heard a few months
ago on a Boost list that clang 5 was getting competitive
with gcc in terms of generating smaller executables. I
didn't find that to be the case with two programs that I
build. They were still significantly larger than their
gcc counterparts.
 

Brian
Ebenezer Enterprises - Enjoying programming again.
http://webEbenezer.net
Jorgen Grahn <grahn+nntp@snipabacken.se>: Sep 17 05:43AM

> from /usr/include/c++/7/iostream:39,
 
> so something has changed since the ancient versions I used before.
 
> Trying various compiler flags -std=c++98 or gnu++98 helps not.
 
Probably a case of a new compiler revealing broken code.
 
Check if your package or some library it uses defines a macro called
'min'. As you can see when you think about it, the code in
stl_algobase.h would become very unhappy if that was the case.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 17 07:35AM +0200

> from /usr/include/c++/7/iostream:39,
 
> so something has changed since the ancient versions I used before.
 
> Trying various compiler flags -std=c++98 or gnu++98 helps not.
 
In C++14 §25.4.7 there are the following overloads of `min`:
 
template<class T> constexpr const T& min(const T& a, const T& b);
 
template<class T, class Compare>
constexpr const T& min(const T& a, const T& b, Compare comp);
 
template<class T>
constexpr T min(initializer_list<T> t);
 
template<class T, class Compare>
constexpr T min(initializer_list<T> t, Compare comp);
 
`min` is not a macro in standard C++.
 
Maybe that fact can guide you towards resolving this?
 
 
Cheers & hth.,
 
- Alf
Manfred <noname@invalid.add>: Sep 18 12:56PM +0200

On 9/17/2017 8:30 PM, Öö Tiib wrote:
 
> Somehow the authors of your package have managed to do same stupid
> thing that Microsoft did. Microsoft did so that the windows.h declared
> macros named "min" and "max" and so standard library did not work.
 
Agreed with the opinion about MS macros
 
> define macros min and max. However Microsoft's own headers like
> gdiplus.h used those min and max macros and so more clever solution
> was needed.
 
There is a trick for this:
#include <algorithm>
(std::min)(17, 29)
 
https://social.msdn.microsoft.com/Forums/vstudio/en-US/9b83c008-7bfc-483d-9395-959d4ccefff6/vs-10-where-is-cppmax-and-cppmin-or-equivalent?forum=vcgeneral
 
(It works with MSVC, never used with gcc)
 
Kan <fcantoro@nospam.it>: Sep 18 02:33PM +0200

Il 18/09/2017 12:56, Manfred ha scritto:
> (std::min)(17, 29)
 
> https://social.msdn.microsoft.com/Forums/vstudio/en-US/9b83c008-7bfc-483d-9395-959d4ccefff6/vs-10-where-is-cppmax-and-cppmin-or-equivalent?forum=vcgeneral
 
> (It works with MSVC, never used with gcc)
 
Yes, but however you still have problems with other calls like
std::numeric_limits<T>::min(), so in this case I solved writing:
 
#include <Windows.h>
#undef min
#undef max
#include <algorithm>
 
Then use std::min and std::max instead on min and max.
Otherwise, if you really don't want to change the source code:
using std::min;
using std::max;
 
JiiPee <no@notvalid.com>: Sep 16 12:33PM +0100

On 16/09/2017 12:28, Öö Tiib wrote:
 
> f();
 
> f(42);
> }
 
 
Thanks, something to try. But I think I really need to use this
std::function here, because the real problem i have is to use it with
templates, so I need to use std::function in my code. Your example does
not use std::function.
 
In my real code i have something like:
 
function f = foo<1>;
 
and foo is a template function with default parameters.
 
So i want to use f:
 
f(1);
 
f();
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 16 06:02PM +0200

On 9/16/2017 12:23 PM, JiiPee wrote:
 
> f(); // does not work
 
> How can I create a fuction object which understands default arguments?
> like I would like to call here f(); but this does not compile. Thanks you
 
There is as far as I know no way to obtain the argument default values
programmatically.
 
As soon as you pass a stand-alone function around, even as a template
parameter, information about the argument defaults is lost.
 
However, when the original function is a member function of a class, a
functor, that /type/ can be passed around, and the function can then be
called with defaults:
 
 
#include <iostream>
#include <memory> // std::(unique_ptr, make_unique)
#include <utility> // std::move
using namespace std;
 
struct Foo
{
void action( int const a = 6 ) const { cout << "Foo " << a << endl; }
};
 
struct Bar
{
void action( int const a = 6 ) const { cout << "Bar " << a << endl; }
};
 
template< class Some_type > struct Type_carrier_ {};
 
struct Abstract_callable
{
virtual void action() const = 0;
virtual void action( int ) const = 0;
virtual auto clone() const -> unique_ptr<Abstract_callable> = 0;
};
 
template< class Type >
struct Specific_callable
: Abstract_callable
{
void action() const override { Type{}.action(); }
void action( int const a ) const override { Type{}.action( a ); }
 
auto clone() const
-> unique_ptr<Abstract_callable>
override
{ return unique_ptr<Abstract_callable>{ new Specific_callable{
*this } }; }
};
 
class Callable
{
private:
unique_ptr<Abstract_callable> impl_;
 
public:
void operator()() const { impl_->action(); }
void operator()( int const a ) const { impl_->action( a ); }
 
template< class Action_provider >
Callable( Type_carrier_<Action_provider> )
: impl_{ make_unique< Specific_callable< Action_provider > >() }
{}
 
Callable( Callable const& other )
: impl_{ other.impl_->clone() }
{}
 
Callable( Callable&& other )
: impl_{ move( other.impl_ ) }
{}
};
 
void do_stuff( Callable const& f )
{
f();
f( 42 );
}
 
auto main()
-> int
{
do_stuff( Callable{ Type_carrier_<Foo>{} } );
do_stuff( Callable{ Type_carrier_<Bar>{} } );
}
 
 
The key point of this code being that `Callable` is a concrete,
non-templated type whose instances carry the requisite knowledge.
 
Instead of separate `operator()` implementations in `Callable`, you can
just define a single templated one that forwards its arguments.
 
 
Cheers & hth.,
 
- Alf
JiiPee <no@notvalid.com>: Sep 16 02:42PM +0100

On 16/09/2017 14:07, SG wrote:
> myfunction mf = { &foo, 6 };
> mf();
> mf(42);
 
 
interesting idea. thanks. Seems like operator() is the way to go. I ll
play around with these
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: