A
arnuld
this is the code:
-------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
struct Pair {
std::string name;
double val;
};
std::vector<Pair> pairs;
double& value(const std::string s) {
for(int i=0; i < pairs.size(); i++)
if(s == pairs.name) return pairs.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@localhost 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_istream<_CharT, _Traits>&
std::basic_istream<_CharT,
[SNIP]
<_CharT, _Traits>:perator>>(void*&) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:230:
note: std::basic_istream<_CharT, _Traits>&
std::basic_istream<_CharT,
_Traits>:perator>>(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT = char, _Traits = std::char_traits<char>]
[arnuld@localhost cpp]$
-------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
struct Pair {
std::string name;
double val;
};
std::vector<Pair> pairs;
double& value(const std::string s) {
for(int i=0; i < pairs.size(); i++)
if(s == pairs.name) return pairs.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@localhost 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_istream<_CharT, _Traits>&
std::basic_istream<_CharT,
[SNIP]
<_CharT, _Traits>:perator>>(void*&) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:230:
note: std::basic_istream<_CharT, _Traits>&
std::basic_istream<_CharT,
_Traits>:perator>>(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT = char, _Traits = std::char_traits<char>]
[arnuld@localhost cpp]$