Stroustrup第5.5节“结构矢量” [英] Stroustrup section 5.5 "vector of struct"
问题描述
这是代码:
------------------------------ -------------------------------------------
>
#include< iostream>
#include< string>
#include< vector>
struct Pair {
std :: string name;
double val;
};
std :: vector< Pairpairs;
double& value(const std :: string s){
for(int i = 0; i< pairs.size(); i ++)
if(s == pair [i] .name)返回对[i] .val;
配对p = {s,-1.1};
pairs.push_back(p) ; //对末尾添加
返回对[pair.size() - 1] .val;
}
int main(){
配对p0 = {" p0",0.0};
配对p1 = {" p1",1.0};
配对p2 = {" p2",2.0};
//添加到配对
pairs.push_back (p0);
pairs.push_back(p1);
pairs.push_back(p2);
const std :: string s1;
std :: cout<< 现在我们将检查\" pairs \":" ;;
std :: cin> s1;
value(s1 );
}
------------------------------- -------------------------------------------
>
基本上它是一个向量 对的我添加了3个对价值。
然后我称之为价值函数返回对val的引用
与string相关&安培; ifstring在对中找不到然后它
只是创建一个新的对 &安培;将其添加到对的末尾。 。
编译给了我这个错误(在BLAG Linux上使用g ++ 4.1.1。):
--------- -------------------------------------------------- --------------
[arnuld @ localhost cpp] $ g ++ 05_55-references.cpp
05_55-references .cpp:在函数''int main()''中:
05_55-references.cpp:41:错误:''运算符>>''在
$ b中不匹配$ b''std :: cin> s1''
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../ include / c ++ / 4.1.1 / istream:131:
注意:候选人是:std :: basic_istream< _CharT,_Traits>&
std :: basic_istream< _CharT ,
[SNIP]
< _CharT,_Traits> :: operator>>(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:
注意:std :: basic_istream< _CharT,_Traits>&
std :: basic_istream< _CharT,
_Traits> :: operator>>(std :: basic_streambuf< _CharT,_Traits> *)[with
_CharT = char,_ Traits = std :: char_traits< char>]
[arnuld @ localhost cpp] $
-------- -------------------------------------------------- ---------------------
出了什么问题?
g ++ 05_55-references.cpp
05_55-references.cpp:在函数''int main()''中:
05_55-references.cpp:41:错误:''运算符>>''不匹配
''std :: cin> s1''
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:131:
注意:候选人是:std :: basic_istream< _CharT,_Traits>&
std :: basic_istream< _CharT,
[SNIP]
< _CharT,_Traits> :: operator>>(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:
注意:std :: basic_istream< _CharT,_Traits>&
std :: basic_istream< _CharT,
_Traits> :: operator>>(std :: basic_streambuf< _CharT,_Traits> *)[含
_CharT = char,_Traits = std :: char_traits< char>]
[arnuld @ localhost cpp]
------------- -------------------------------------------------- ----------------
出了什么问题?
" arnuld" < ar ***** @ gmail.com写信息
news:11 ********************** @ m7g2000cwm。 googlegro ups.com ...
这是代码:
备注在线e
>
------------------------- ------------------------------------------------ <无线电通信/>
#include< iostream>
#include< string>
#include< vector>
[...]
const std :: string s1;
因为你在这里使用''const''qualifer,那就是'
你承诺你的代码将*不*修改
对象''s1''。
std :: cout<< 现在我们将检查\对\":" ;;
std :: cin> s1;
....但是你在这里尝试修改''s1''。 :-)
>
value(s1);
}
[...]
05_55-references.cpp:在函数''int main()''中:
05_55-references.cpp:41:错误:''运算符>>''不匹配
''std :: cin> s1''
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/istream:131:
注意:候选人是:std :: basic_istream< _CharT,_Traits>&
std :: basic_istream< _CharT,
[...]
出了什么问题?
您正在尝试修改const对象。不允许。
从's1'的定义中删除''const''。
我怀疑这只是一个'心不在焉' ''你做了什么,而你
确实理解'const'的意思。如果没有,请说明,我们会解释。
-Mike
this is the code:
-------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
struct Pair {
std::string name;
double val;
};
std::vector<Pairpairs;
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@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>::operator>>(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>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT = char, _Traits = std::char_traits<char>]
[arnuld@localhost cpp]$
-------------------------------------------------------------------------------
what is wrong?
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>::operator>>(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>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT = char, _Traits = std::char_traits<char>]
[arnuld@localhost cpp]
-------------------------------------------------------------------------------
what is wrong?
"arnuld" <ar*****@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.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_istream<_CharT, _Traits>&
std::basic_istream<_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
这篇关于Stroustrup第5.5节“结构矢量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!