473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Qs on 'late construction' and operator >>



Hi all

I have a class that contains as one of it's members a type that is
internally constructed like this:

std::vector< std::valarray< float > > foo

Think of it as a matrix. I cannot construct foo when the class itself is
constructed because I don't know how big it needs to be until I have loaded
that information from a file.

Within the member function that loads the file (including the information r
& c), this compiles OK:

std::vector< std::valarray< float > > temp( r, std::valarray<float>( c ));
foo = temp;

....but I'm sure there is a more elegant/efficient way to do this on foo
directly - any tips on this?

I have defined access to this type like a Fortran array i.e. foo( i, j )
either reads from or writes to the j'th valarray element of the i'th vector.
This works fine in general usage but, not when I am trying to load from the
input file in a loop:

std::ifstream infile( from_path );
// in i, j, loop
infile >> foo( i, j );

Again, any tips welcomed.

TIA

Michael
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #1
6 1222
On Mon, 2005-05-16 at 10:47 -0400, Michael Hopkins wrote:

Hi all

I have a class that contains as one of it's members a type that is
internally constructed like this:

std::vector< std::valarray< float > > foo

Think of it as a matrix. I cannot construct foo when the class itself is
constructed because I don't know how big it needs to be until I have loaded
that information from a file.

Within the member function that loads the file (including the information r
& c), this compiles OK:

std::vector< std::valarray< float > > temp( r, std::valarray<float>( c ));
foo = temp;

...but I'm sure there is a more elegant/efficient way to do this on foo
directly - any tips on this?
Seems like a smart approach to me.

However replacing :
foo = temp
with:
std::swap(foo, temp)
will probably be more efficient, since it will avoid allocating memory
twice and copying. I don't know if this will matter to you or not,
considering that you then read the contents from disk.
I have defined access to this type like a Fortran array i.e. foo( i, j )
either reads from or writes to the j'th valarray element of the i'th vector.
This works fine in general usage but, not when I am trying to load from the
input file in a loop:

std::ifstream infile( from_path );
// in i, j, loop
infile >> foo( i, j );
What compiler error or unwanted behaviour do you get?

Post code.
Again, any tips welcomed.

TIA

Michael

Jul 23 '05 #2
std::vector< std::valarray< float > > foo;
// load your info here
foo.resize(r, std::valarray<float>( c));
---
1) why don't u use foo[i][j] operators ?
2) It should works fine, I think something wrong in your defined access
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #3
Michael Hopkins wrote:
Hi all

I have a class that contains as one of it's members a type that is
internally constructed like this:

std::vector< std::valarray< float > > foo

Think of it as a matrix. I cannot construct foo when the class
itself is constructed because I don't know how big it needs to be
until I have loaded that information from a file.

Within the member function that loads the file (including the
information r & c), this compiles OK:

std::vector< std::valarray< float > > temp( r, std::valarray<float>( c )); foo = temp;

...but I'm sure there is a more elegant/efficient way to do this on
foo directly - any tips on this?

[snip]

You could use a (smart) pointer type for 'foo' instead of making it a
local object. Have the class' constructor assign a null pointer to
'foo'. Treat this as a sort of "zombie" state for the object, i.e.,
the object isn't fully usable until the data is loaded from the file.
Then, once you have the necessary information, you can instantiate
'foo':

foo=new std::vector<std::valarray<float> >(r,std::valarray<float>(c));

Note that the above line assumes that foo is a raw pointer type.
You'll have to modify it if you use a smart pointer.

Kristo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #4
Michael Hopkins wrote:
I have a class that contains as one of it's members a type
that is internally constructed like this: std::vector< std::valarray< float > > foo Think of it as a matrix. I cannot construct foo when the
class itself is constructed because I don't know how big it
needs to be until I have loaded that information from a file.
No problem. If you use the default constructor, the vector is
empty.
Within the member function that loads the file (including the
information r & c), this compiles OK: std::vector< std::valarray< float > > temp( r, std::valarray<float>( c )); foo = temp; ...but I'm sure there is a more elegant/efficient way to do
this on foo directly - any tips on this?
I don't have any problem with doing it this way, although it can
be relatively inefficient if the array is large.
I have defined access to this type like a Fortran array
i.e. foo( i, j ) either reads from or writes to the j'th
valarray element of the i'th vector. This works fine in
general usage but, not when I am trying to load from the input
file in a loop: std::ifstream infile( from_path );
// in i, j, loop
infile >> foo( i, j ); Again, any tips welcomed.


I'm not sure how you could define access to the foo object like
this -- you can't add functions to std::vector, and the
operator()() must be a member.

Anyway, with regards to your problem, the obvious solution would
be to use push_back on the vector for each line of data you
read. With regards to the valarray part, I have relatively
little experience; I think you'll have to experiment to find out
whether it is faster to use a local valarray, then push_back it,
or to push_back an empty (default constructed) valarray, then
use resize on the element in the vector.

I might add that if you need a Fortran compatible array, this
will not do the job. For that, you'll probable have to use a
simple std::vector< float >, and manage the dimensions yourself.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #5


Thanks for the input guys.

The >> problem was a dumb thing on my part - fixed easily.

For the 'late construction' part I experimented with the smart pointer/new
approach as some suggested, but ended up initialising the:

std::vector< std::valarray< float > > foo;

...as a placeholder and then using the built in .resize() abilities of both
vector and valarray appropriately. This turns out to be fast and means I
don't have to mess about with memory allocation/deallocation myself. I have
a preference for avoiding pointers and using the STL as much as possible to
let other people deal with the headaches!

Someone asked about the ( i, j ) access - I do this on all my linear
algebra/maths code to mimic Fortran syntax from 1 to n, just as I did before
with C (but with different syntax in that case). One way is to use:

valarray<T> v( rows * cols );

...for storage and set up another valarray of pointers (uo_row_ptr) to the
start of each 'row' in the matrix and then:

T & operator ()( const iter r, const iter c )
{
return *(uo_row_ptr( r )+c);
};

There are many options but this one works well, usually just as fast as or
sometimes even faster than raw array access with a good compliler and
optimisation in fact (especially when you use valarray).

It allows me to easily escape the dreaded and almost universal requirement
amongst the C++ community to do access/loops from 0 to n-1 which is so
unnatural for mathematical work. This is one of the main reasons why I
don't use some of the powerful libraries like uBlas, MTL and Blitz.

M
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #6
mi*************@hopkins-research.com (Michael Hopkins) wrote (abridged):
I cannot construct foo when the class itself is constructed because
I don't know how big it needs to be until I have loaded that
information from a file.
Would it help to pass the input stream to the class's constructor?

std::vector< std::valarray< float > > temp( r, std::valarray<float>( c
));

foo = temp;


Replacing the assignment with:
foo.swap( temp );

ought to be an efficient solution. Vector's default constructor will
probably not allocate any memory, and the swap won't either.

-- Dave Harris, Nottingham, UK.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Yu Lianqing | last post by:
Hi, all I am writing an overloading operator >> function for a template class and can't make it right. G++ 3.2 (Redhat8.0) gives the following errors: g++ -c list.cxx In file included from...
6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
7
by: John M | last post by:
Can someone tell how to fix my visual c++ so it can discard this error message. I get this error message error C2593: 'operator >>' is ambiguous But I do not when I am on my mandrake linux...
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
2
by: Bill | last post by:
I am trying to convert a Java APP to C# and it appears C# does not have a >>> operator. Does anyone the best way to conver this operator in to c#? Thanks. -- Bill
7
by: Peter Jansson | last post by:
Dear group, I have been struggling to get a simple program for inserting and extracting std::tm objects to/from streams to work. The code below tries to read a std::tm object from a...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.