473,729 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stroustrup section 5.5 "vector of struct"

this is the code:

-------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <vector>

struct Pair {
std::string name;
double val;
};

std::vector<Pai rpairs;

double& value(const std::string s) {
for(int i=0; i < pairs.size(); i++)
if(s == pairs[i].name) return pairs[i].val;

Pair p = {s, -1.1};
pairs.push_back (p); // pair added at the end

return pairs[pairs.size() - 1].val;
}
int main() {

Pair p0 = {"p0", 0.0};
Pair p1 = {"p1", 1.0};
Pair p2 = {"p2", 2.0};

// add to "pairs"
pairs.push_back (p0);
pairs.push_back (p1);
pairs.push_back (p2);

const std::string s1;
std::cout << "now we will check \"pairs\": ";
std::cin >s1;

value(s1);
}
--------------------------------------------------------------------------

basically it is a "vector" of "Pair" to which i added 3 "Pair values".
then i called "value" function which returns a reference to "val"
related to "string" & if "string" is not found in "pairs" then it
simply creates a new "Pair" & adds it to the end of "pairs" .
compilation gave me this error (using "g++ 4.1.1" on BLAG Linux. ):

-------------------------------------------------------------------------
[arnuld@localhos t cpp]$ g++ 05_55-references.cpp

05_55-references.cpp: In function 'int main()':
05_55-references.cpp: 41: error: no match for 'operator>>' in
'std::cin >s1'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:131:
note: candidates are: std::basic_istr eam<_CharT, _Traits>&
std::basic_istr eam<_CharT,

[SNIP]

<_CharT, _Traits>::opera tor>>(void*&) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:230:
note: std::basic_istr eam<_CharT, _Traits>&
std::basic_istr eam<_CharT,
_Traits>::opera tor>>(std::basi c_streambuf<_Ch arT, _Traits>*) [with
_CharT = char, _Traits = std::char_trait s<char>]

[arnuld@localhos t cpp]$
-------------------------------------------------------------------------------

what is wrong?

Nov 8 '06 #1
13 2085

"arnuld" <ar*****@gmail. comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
this is the code:
Remarks in-line e
>
-------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <vector>
[...]
const std::string s1;
Since you used the 'const' qualifer here, that's
your promise that your code will *not* modify
the object 's1'.
std::cout << "now we will check \"pairs\": ";
std::cin >s1;
.... but here you go trying to modify 's1'. :-)
>
value(s1);
}
[...]
05_55-references.cpp: In function 'int main()':
05_55-references.cpp: 41: error: no match for 'operator>>' in
'std::cin >s1'
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:131:
note: candidates are: std::basic_istr eam<_CharT, _Traits>&
std::basic_istr eam<_CharT,
[...]
what is wrong?
You're trying to modify a const object. Not allowed.
Remove 'const' from your definition of 's1'.

I suspect this was just an 'absent-minded' thing you did, and you
do understand what 'const' means. If not, say so, and we'll explain.

-Mike
Nov 8 '06 #2

arnuld wrote in message
<11************ **********@m7g2 000cwm.googlegr oups.com>...
>this is the code:

-------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <vector>

struct Pair {
std::string name;
double val;
};

std::vector<Pa irpairs;

double& value(const std::string s) {
for(int i=0; i < pairs.size(); i++)
if(s == pairs[i].name) return pairs[i].val;

Pair p = {s, -1.1};
pairs.push_back (p); // pair added at the end

return pairs[pairs.size() - 1].val;
}
int main() {

Pair p0 = {"p0", 0.0};
Pair p1 = {"p1", 1.0};
Pair p2 = {"p2", 2.0};

// add to "pairs"
pairs.push_back (p0);
pairs.push_back (p1);
pairs.push_back (p2);

const std::string s1;
std::cout << "now we will check \"pairs\": ";
std::cin >s1;

value(s1);
}
--------------------------------------------------------------------------

basically it is a "vector" of "Pair" to which i added 3 "Pair values".
then i called "value" function which returns a reference to "val"
related to "string" & if "string" is not found in "pairs" then it
simply creates a new "Pair" & adds it to the end of "pairs" .
compilation gave me this error (using "g++ 4.1.1" on BLAG Linux. ):

-------------------------------------------------------------------------
[arnuld@localhos t cpp]$ g++ 05_55-references.cpp

05_55-references.cpp: In function 'int main()':
05_55-references.cpp: 41: error: no match for 'operator>>' in
'std::cin >s1'

[SNIP]
[arnuld@localhos t cpp]$
----------------------------------------------------------------------------
---
>
what is wrong?
Look at std::string 's1'.
const std::string s1;
The 'const' means you (or anything else in your program) is allowed to change
it. Remove the 'const', and see if it will work.

Usually you would use a const string like:

const std::string name( "This will stay the same for the whole program" );

--
Bob R
POVrookie
Nov 8 '06 #3

BobR wrote in message ...
>
>>what is wrong?

Look at std::string 's1'.
> const std::string s1;

The 'const' means you (or anything else in your program) is allowed to
change
>it. Remove the 'const', and see if it will work.
The 'const' means you (or anything else in your program) is NOT allowed to
change it. Remove the 'const', and see if it will work.

--
Bob R
POVrookie
Nov 8 '06 #4
Mike Wahler wrote:
news:11******** **************@ m7g2000cwm.goog legroups.com...
this is the code:
Remarks in-line e
what does that mean?
You're trying to modify a const object. Not allowed.
Remove 'const' from your definition of 's1'.
I suspect this was just an 'absent-minded' thing you did, and you
do understand what 'const' means. If not, say so, and we'll explain.
i do understand what "const" means but that was really absent-minded
thing i did. anyway i removed it & programme compiled & ran but it does
not do what i intended. i also made some minor changes to function
"value" to get output on my terminal:

-----------------------------------------------------------
double& value(const std::string s) {
for(int i=0; i < pairs.size(); i++)
if(s == pairs[i].name)
{
std::cout << pairs[i].val << "\n";
return pairs[i].val;
}

Pair p = {s, -1.1};
pairs.push_back (p); // pair added at the end
std::cout << pairs[pairs.size() - 1].val << "\n";
}
-------------------------------------------------------------
------------------- OUTPUT --------------------------------
[arnuld@localhos t cpp]$ ./a.out
now we will check "pairs": p2
2
[arnuld@localhos t cpp]$ ./a.out
now we will check "pairs": p9
-1.1
[arnuld@localhos t cpp]$
---------------------------------------------------------------

function "value" returns a "double&" not "int". then why did i get /2/
which is of type /int/ instead of double /2.0/?

Nov 8 '06 #5
Hi arnuld,

If u want to view decimal precision values, u need to set some
properties in cout

try using these statments

cout << showpoint;
cout.precision( 2);

Regards.
-Amit Gupta

arnuld wrote:
Mike Wahler wrote:
news:11******** **************@ m7g2000cwm.goog legroups.com...
this is the code:
Remarks in-line e

what does that mean?
You're trying to modify a const object. Not allowed.
Remove 'const' from your definition of 's1'.
I suspect this was just an 'absent-minded' thing you did, and you
do understand what 'const' means. If not, say so, and we'll explain.

i do understand what "const" means but that was really absent-minded
thing i did. anyway i removed it & programme compiled & ran but it does
not do what i intended. i also made some minor changes to function
"value" to get output on my terminal:

-----------------------------------------------------------
double& value(const std::string s) {
for(int i=0; i < pairs.size(); i++)
if(s == pairs[i].name)
{
std::cout << pairs[i].val << "\n";
return pairs[i].val;
}

Pair p = {s, -1.1};
pairs.push_back (p); // pair added at the end
std::cout << pairs[pairs.size() - 1].val << "\n";
}
-------------------------------------------------------------
------------------- OUTPUT --------------------------------
[arnuld@localhos t cpp]$ ./a.out
now we will check "pairs": p2
2
[arnuld@localhos t cpp]$ ./a.out
now we will check "pairs": p9
-1.1
[arnuld@localhos t cpp]$
---------------------------------------------------------------

function "value" returns a "double&" not "int". then why did i get /2/
which is of type /int/ instead of double /2.0/?
Nov 8 '06 #6
Amit wrote:
Hi arnuld,

If u want to view decimal precision values, u need to set some
properties in cout

try using these statments

cout << showpoint;
cout.precision( 2);
1st, do not top-post.

2nd, you did not get my point. my input was "2.0" ( Pair p2 = { "p2",
2.0} )

i wanted to know why compiler changed it to /int 2/. on the contrary,
if i input / Pair p3 = { "p3", 3.3} / i get 3.3 as output. so this time
compiler did not change it to /int 3/. why so?

Nov 8 '06 #7

Amit wrote in message
<11************ **********@h54g 2000cwb.googleg roups.com>...
>Hi arnuld,

If u want to view decimal precision values, u need to set some
properties in cout

try using these statments

cout << showpoint;
cout.precision( 2);

Regards.
-Amit Gupta
Nice top-post. Been takeing lessons long?
(http://www.parashift.com/c++-faq-lit....html#faq-5.4).

A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #8

arnuld wrote in message ...
>Amit wrote:
>Hi arnuld,
If u want to view decimal precision values, u need to set some
properties in cout

try using these statments

cout << showpoint;
cout.precision (2);

1st, do not top-post.

2nd, you did not get my point. my input was "2.0" ( Pair p2 = { "p2",
2.0} )

i wanted to know why compiler changed it to /int 2/. on the contrary,
if i input / Pair p3 = { "p3", 3.3} / i get 3.3 as output. so this time
compiler did not change it to /int 3/. why so?
What evidence do you have that it is an 'int'? What you see in the output may
be very different than what the variable holds.

Put this in main() and try it:

{
using std::cout;
cout.setf( std::ios_base:: fixed );
cout.precision( 20 );
double d1 = 8.126e9;
cout <<"d1 = "<< d1 <<std::endl;

cout.setf( std::ios_base:: scientific );
cout.precision( 2);
cout <<"d1 = "<< d1 <<std::endl;
cout.precision( 6);
cout <<"d1 = "<< d1 <<std::endl;
cout.setf( std::ios_base:: fixed );
}

--
Bob R
POVrookie
Nov 8 '06 #9
BobR wrote:
What evidence do you have that it is an 'int'?
becuase i get plain 2 ( not 2.0 )
What you see in the output may
be very different than what the variable holds.
then how will i get the desired output? or how will i know what the
variable holds?
2nd, is it really necessary, practically, to know answers to these 2
questions i have asked?

BTW, what is the "type" of "2" (the output i got)?
Put this in main() and try it:

{
using std::cout;
cout.setf( std::ios_base:: fixed );
cout.precision( 20 );
double d1 = 8.126e9;
cout <<"d1 = "<< d1 <<std::endl;

cout.setf( std::ios_base:: scientific );
cout.precision( 2);
cout <<"d1 = "<< d1 <<std::endl;
cout.precision( 6);
cout <<"d1 = "<< d1 <<std::endl;
cout.setf( std::ios_base:: fixed );
}
this is the output from "g++ 4.1.1 on BLAG Linux":

-------------------------------------------------------------------
[arnuld@localhos t ~]$ ./a.out
d1 = 8126000000.0000 000000000000000 0
d1 = 8.1e+09
d1 = 8.126e+09
[arnuld@localhos t ~]$
---------------------------------------------------------------------

you have used what "Amit" told to me: / cout.precision( n) / with your
mysterious / cout.setf( std::ios_base:: fixed ); /

Nov 8 '06 #10

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

Similar topics

1
2504
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
1
2360
by: G Fernandes | last post by:
Hi, can someone tell me what the following words mean as per C/clc: 1) token 2) token sequence 3) scalar variable 4) vector
3
14502
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
10
2738
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
4
5704
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a vector
11
2135
by: arnuld | last post by:
this is the code which runs without any trouble: ----------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Entry { std::string name; int e_num;
8
40469
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am familiar with "typedef int NUMBER", but how does it work with structures. Tilak
15
2677
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it then adds values to "jd" 3.) in the end it prints values of "jd".
4
7093
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 Expression:vector subscript out of range.
0
8763
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
9284
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
9202
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
9148
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.