473,804 Members | 3,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::opera tor
(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<boo l, _Alloc>::operat or[](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<bo ol, _Alloc>::operat or[] [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<bo ol, _Alloc>::operat or[] [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 2953
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>::o perator[] returns 'bool'
(the value of the first object, by-value).
18 cin >> (vi[0]);
vi[0] is an lvalue because vector<int>::op erator[] 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::opera tor
(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>::o perator[] 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<>::opera tor[] 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>::o perator[] 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<>::opera tor[] 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
4250
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
3320
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 contain several algorithms including the following ones. ================================== ifstream ifs; // input file stream string ret_str;
28
1959
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 for criticism, this time dead serious. I really need an effective Vector emulator for a project (as much effective as something not implemeted in language but emulated by means of the language itself is: a
2
2370
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 perform operations for only those but unable to get how to do that. Can someone help please. I want Vector to work with any datatype. template<class T>
2
1519
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
2172
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 of the input file is to be given on the command line. * All negative values in the vector shall then be replaced with their positive counterpart, using the standard algorithm for_each.
6
3950
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 number of words in the input, and to count how many times each word occurred.
4
2285
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 the integers read in that line. There will be a single vector<vector<ItemType>object that will stores all of the vector<ItemTypeobjects mentioned in the previous sentence. I wrote a quick implementation and it seems to be working, but i would like...
1
1952
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 in vector.h file and I have included Vector.h file in my .hpp file(where I have designed my class template for Vector) But still this error is persistent. Vector.hpp:271:70: error: ‘bool Vector<T>::operator==(const Vector<T>&, const...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.