473,405 Members | 2,344 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,405 software developers and data experts.

Reading from an istream to a bool vector

Hello,

With this code:

$ cat -n ifs.cc
1 #include <vector>
2 #include <iostream>
3
4 using std::vector;
5 using std::cin;
6
7 int
8 main()
9 {
10 vector< bool > vb(1);
11 vector< int > vi(1);
12 bool b;
13 int i;
14
15 cin >> b;
16 cin >> i;
17 cin >> (vb[0]);
18 cin >> (vi[0]);
19 return 0;
20 }

I have the following errors:

$ g++-2.95.3 -Wall ifs.cc
ifs.cc: In function `int main()':
ifs.cc:17: initialization of non-const reference type `bool &'
ifs.cc:17: from rvalue of type `bool'
/.../iostream.h:217: in passing argument 1 of `istream::operator
(bool &)'


$ g++-3.3.2 -Wall ifs.cc
ifs.cc: In function `int main()':
ifs.cc:17: error: no match for 'operator>>' in 'std::cin >>
std::vector<bool, _Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](0)'

$ g++-3.4.0 -Wall ifs.cc
ifs.cc: In function `int main()':
ifs.cc:17: error: no match for 'operator>>' in 'std::cin >>
(&vb)->std::vector<bool, _Alloc>::operator[] [with _Alloc =
std::allocator<bool>](0u)'

$ g++-3.4.1 -Wall ifs.cc
ifs.cc: In function `int main()':
ifs.cc:17: error: no match for 'operator>>' in 'std::cin >>
(&vb)->std::vector<bool, _Alloc>::operator[] [with _Alloc =
std::allocator<bool>](0u)'

So i guess this is the good behavior, but does anybody have a
rationale for that? Why can i read into an int vector but not a bool
one??

--
Nomak

Jul 22 '05 #1
4 2921
On Sat, 07 Aug 2004 12:37:30 -0700, Nomak wrote:
$ cat -n ifs.cc
1 #include <vector>
2 #include <iostream>
3
4 using std::vector;
5 using std::cin;
6
7 int
8 main()
9 {
10 vector< bool > vb(1);
11 vector< int > vi(1);
12 bool b;
13 int i;
14
15 cin >> b;
b is an lvalue, we can pass it as a non-const reference to
operator>>.
16 cin >> i;
17 cin >> (vb[0]);
vb[0] is an rvalue because vector<bool>::operator[] returns 'bool'
(the value of the first object, by-value).
18 cin >> (vi[0]);
vi[0] is an lvalue because vector<int>::operator[] returns 'int&'
(the first object by-reference).
19 return 0;
20 }

I have the following errors:

$ g++-2.95.3 -Wall ifs.cc
ifs.cc: In function `int main()':
ifs.cc:17: initialization of non-const reference type `bool &'
ifs.cc:17: from rvalue of type `bool' /.../iostream.h:217: in passing
argument 1 of `istream::operator
(bool &)'

[...]
So i guess this is the good behavior, but does anybody have a rationale
for that? Why can i read into an int vector but not a bool one??


There is a specialization for vector<bool> where the return type
of operator[] differs from the return value of operator[] of the
general vector implementation.

vector<bool> is special in that it uses single bits to store the
bool objects. In a typical implementation, an unsigned int may be
used as the underlying data type. On a 32-bit machine this might
have the first 32 objects of the container share one unsigned
int.

Since it is not possible to return a reference to a single bit,
operator[] has to return by-value in the vector<bool>
specialization.

Ali
Jul 22 '05 #2
Ali Cehreli wrote:
[...]

So i guess this is the good behavior, but does anybody have a
rationale for that? Why can i read into an int vector but not a
bool one??


There is a specialization for vector<bool> where the return type
of operator[] differs from the return value of operator[] of the
general vector implementation.

vector<bool> is special in that it uses single bits to store the
bool objects. In a typical implementation, an unsigned int may be
used as the underlying data type. On a 32-bit machine this might
have the first 32 objects of the container share one unsigned
int.

Since it is not possible to return a reference to a single bit,
operator[] has to return by-value in the vector<bool>
specialization.


Thanks. It makes my loading code ugly...

--
Nomak
Jul 22 '05 #3
>> 16 cin >> i;
17 cin >> (vb[0]);
vb[0] is an rvalue because vector<bool>::operator[] returns 'bool'
(the value of the first object, by-value).


[...]
Since it is not possible to return a reference to a single bit,
operator[] has to return by-value in the vector<bool>
specialization.


That might not be that simple, otherwise it would be impossible to
change any value in that bool array : if vb[i] is rvalue, we can't
assign anything to it.

If I'm not mistaken, the vector<>::operator[] method returns an object
of type named "reference", which (in the case of bool vector) is
probably some kind of proxy object for the targetted array element.

Not too sure though, but I can't think of anything else that would do
the job...

Sylvain.
Jul 22 '05 #4
On Fri, 13 Aug 2004 17:47:11 -0700, Sylvain Audi wrote:
16 cin >> i;
17 cin >> (vb[0]);
vb[0] is an rvalue because vector<bool>::operator[] returns 'bool' (the
value of the first object, by-value).


[...]
Since it is not possible to return a reference to a single bit,
operator[] has to return by-value in the vector<bool> specialization.


That might not be that simple, otherwise it would be impossible to
change any value in that bool array : if vb[i] is rvalue, we can't
assign anything to it.


Thank you for the correction.
If I'm not mistaken, the vector<>::operator[] method returns an object
of type named "reference", which (in the case of bool vector) is
probably some kind of proxy object for the targetted array element.


Even when writing the above I was thinking about the posibility of such
a proxy but didn't pay much attention to it. A quick test of mine made
me believe that the operator>> for the proxy can do what the OP
needed. I am not sure why it's not implemented that way.

In any case, googling for older posts show that vector<bool> does not
satisfy some of the container requirements anyway.

Ali
Jul 22 '05 #5

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

Similar topics

7
by: ragi | last post by:
Short version of my program: ifstream File; File.open("test.txt"); if(!File.good()) return; Func(File); <--cannot convert parametr 1 from 'std::ifstream' to 'std::istream'
1
by: Alex Vinokur | last post by:
Testsuites "Comparative Performance Measurement. Reading file into string" at http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1 http://groups.google.com/group/sources/msg/27a9b6f91239c909...
28
by: VK | last post by:
A while ago I wrote a "Vector data type" script using DOM interface to select.options. That was a (beautiful) mind game :-) rather than a practical thing to use. Here is another attempt open...
2
by: vermarajeev | last post by:
I'm unable to use char* or string in Vector class. I know what the problem is but unable to find a proper answer. One solution is like I can call explicit intialization for char* or string and...
2
by: robertoviperbr | last post by:
Hello to everybody. I have a doubt see the code: #include <vector> #include <iostream> #include <string> #include <algorithm> #include <sstream> #include <fstream> #include <iomanip>
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
4
by: zr | last post by:
Hi, i need to read a text file which contains a list of items, all of type ItemType, separated by whitespace and newlines. For each line, there will be a vector<ItemTypeobject that will store...
1
by: abhinuke | last post by:
So here's the thing....I ve been stuck with these errors for almost one and a half day.I couldnt find any solutions.I am working on class template of Vector for its functionality....i have interfaces...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.