|
You're twisting the semantics of istream_iterator s: You only can do the following with such an iterator:Check for the end of stream with the expression it==istream_iterator<int>() .retrieve a value from the associated istream with an expression of the form x=*it; .
Increment the iterator with ++it or it++ .Moreover, after retrieving a value, you must increment the iterator previous to further retrievals. That's it, do not assume you can do much else with such iterators. Do no think these iterators are something like pointers to the data contained in the file, because they are not, hence the odd results when testing r and s for equality. istream_iterator is designed as a convenient adaptor to take advantage of generic algoritms, as such its functionality is strictly limitied to what these algorithms require.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks, Joaquín buddies!
Such piece of codes are taken from a turorial
and it is a concept-proof sample.
It really doubted me. So, if you can explain it
to me, I will be really thankful. I am a newbie
of STL.
Cheers,
George
|
|
|
|
|
Hi, everyone!
Here is a simply STL program deals with copy
algorithm of STL. I do not understand what is the
meaning of the socond parameter of my case, i.e.
--------
istream_iterator<int> ()
--------
What does it mean? Here are the source codes
and related output.
Source codes,
--------
#include <iterator>
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
//0 1 2 3 are in c:\\file1.txt
ifstream ifile ("c:\\file1.txt");
copy (istream_iterator <int> (ifile),
istream_iterator<int> (),
ostream_iterator<int> (cout) );
}
--------
Output,
--------
0123
--------
Thanks in advance,
George
|
|
|
|
|
istream_iterator() indicates end of a stream.
Kuphryn
|
|
|
|
|
Hi, Kuphryn buddies!
As you mentioned, istream_iterator<int> () is an "end-of-stream" iterator. But it is the end of which stream? Why it is assigned as
the end of the ifile stream? Since there are no parameter in istream_iterator<int> (), it really doubted me a lot. (If it has a parameter identifies the ifile, it would be clear, I think.)
Thank in advance,
George
|
|
|
|
|
Its the end of all istream_iterator<int>'s. Think of it as a constant.
Ryan.
They read good books, and quote, but never learn
a language other than the scream of rocket-burn.
Our straighter talk is drowned but ironclad;
elections, money, empire, oil and Dad.
|
|
|
|
|
Thanks, Ryan buddies!
George
|
|
|
|
|
First off: STL RULES!
Ok, now a confession...
the only reason I even use MFC is for CString. I've been searching for a decent stl replacement, and found a few, but I thought I'd ask some STL zealots for their respective opinions...
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
A quick & dirty replacement is:
typedef std::basic_string<TCHAR> tstring; although I think I've seen a true STL port here on CP.
--Mike--
The Internet is a place where absolutely nothing happens.
-- Strong Bad
1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
Michael Dunn wrote:
typedef std::basic_string<TCHAR> tstring;
That's not enough. CString uses reference counting remember?
Best regards,
Alexandru Savescu
P.S. Interested in art? Visit this!
|
|
|
|
|
Well, depending on your application, reference counting can be slower. Herb Sutter did a study and found that reference counting based strings only benefit the application if you application is primarily copying the string from place to place without modification. But you also have to take into account any memory savings that you might get with a reference counting system.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Actually, to be even more specific, I use CString mainly for the method:
szMyString.Format("My unsigned int is 0x%.8X", uiMyInt);
...and for some reason sprintf() scares me
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
std::ostringstream oss;
oss << "My unsigned int is 0x" << std::uppercase << std::setw ( 8 )
<< std::setfill ( '0' ) << std::hex << n << std::endl;
std::cout << oss.str () << std::endl;
Is roughly the equivalent when using the STLs. It a bit of a mouth full, but some of the modifiers persist between calls--std::uppercase will, while std::setw must be called each time--so once you called them, you need not do so again untill you need a new option set.
cheers,
-B
|
|
|
|
|
Ben Burnett wrote:
It a bit of a mouth full,
I agree. Maybe I will have to attempt my own STL replacement for CString...
hmmm, possible article
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
Nitron wrote:
Maybe I will have to attempt my own STL replacement for CString...
Before you dig in, check out the Boost Format library[^]. It might just be what you're looking for.
cheers,
-B
|
|
|
|
|
Nitron wrote:
Maybe I will have to attempt my own STL replacement for CString...
Sounds like more than one nights worth of work.
Nick Parker
The only man who never makes a mistake is the man who never does anything. - Theodore Roosevelt
|
|
|
|
|
I have code which looks like this:
class A<br />
{<br />
std::vector<B* > m_bs;<br />
}
Class A contains a few variables that decide the sort order. I want to create a predicate function to use with sort and lower_bound, which makes use of these parameters in class A.
How do I use a member function of class A as a predicate in sort and lower_bound for operations on a vector of class B objects.
My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
modified 29-Aug-18 21:01pm.
|
|
|
|
|
Just provide a reference to an object of type A to the sort predicate:
class A{
std::vector<B*> m_bs;
struct sort_pB
{
sort_pB(const A& a):a(a){}
bool operator()(B* const pb1,B* const pb2) const
{
}
const A& a;
}
void sort()
{
m_bs.sort(m_bs.begin(),m_bs.end(),sort_pB(*this);
}
};
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
|
|
Hi,
I'm using a VC++ 6.0 ATL COM object in a .NET code. The library is registered before usign it.
Everything work correctly if the library is being accessed continuously; the problem appears when it's not accessed for a while (maybe 5 minutes, it has not been completely assumed) so when it's accessed again it returns an exception of a QueryInterface error for the library interface, although the library has not been modified, neither changed or deleted...nothing! the system only has been forgotten for a while...
Any suggestion or good idea?, any knowledge about it?
Thank you in advance,
Edgar
__________________________________________
Edgar Berengena Moreno
Software Engineer
Appeyron Research
|
|
|
|
|
Are you sure that you keep atleast one reference to the object?
Sounds like the object have been recycled...
"Well I'm just a hard working corporate slave, my mind should hate what my body does crave.
Well I'm just a humble corporate slave, driving myself into a corporate grave"
Corporate Slave, SNOG
|
|
|
|
|
Thanks, Stefan,
It's said it's a hidden bug almost solved from Microsoft. There's a hotfix still not published (Q325699) which you can ask for to Microsoft Support, but it's only available for NET english version.
This bug consists of an incorrect behaviour of the garbage collector, which removes the instance of the COM objects unsued after a few time.
I'll try to solve it manually until Microsoft publish the fix for my spanish version, accessing the object between short periods of times.
Thank you again,
Edgar
__________________________________________
Edgar Berengena Moreno
Software Engineer
Appeyron Research
|
|
|
|
|
Hi, everyone!
I meet with the following errors when compiling the
following simple codes. My IDE is VC 6.0.
Here are the codes.
--------
#include <iterator>
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
ifstream ifile ("c:\\file1.txt");
istream_iterator<int, ptrdiff_t=""> r (ifile);
istream_iterator<int, ptrdiff_t=""> s (ifile);
}
--------
Here are the errors.
--------
Compiling...
testSTL.cpp
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MyProjects\testSTL\testSTL.cpp(11) : error C2664: '__thiscall std::istream_iterator<int,int,struct std::char_traits<int=""> >::std::istream_iterator<int,int,struct std::char_traits<int=""> >(class std::basic_istrea
m<int,struct std::char_traits<int=""> > &)' : cannot convert parameter 1 from 'class std::basic_ifstream<char,struct std::char_traits<char=""> >' to 'class std::basic_istream<int,struct std::char_traits<int=""> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MyProjects\testSTL\testSTL.cpp(12) : error C2664: '__thiscall std::istream_iterator<int,int,struct std::char_traits<int=""> >::std::istream_iterator<int,int,struct std::char_traits<int=""> >(class std::basic_istrea
m<int,struct std::char_traits<int=""> > &)' : cannot convert parameter 1 from 'class std::basic_ifstream<char,struct std::char_traits<char=""> >' to 'class std::basic_istream<int,struct std::char_traits<int=""> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.
testSTL.obj - 2 error(s), 0 warning(s)
--------
How to resolve the trouble?
Thanks in advance,
George
|
|
|
|
|
Next time, check the "Display this message as-is (no HTML)" ooption so that the < and > characters will appear.
You're supplying nonsensical parameters to istream_iterator . Replace with:
istream_iterator<int> r (ifile);
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|