请问如何读出std::vector > 类容器中的值对?

limh821 2010-05-18 04:36:10

大家好! 以下是我主程序的部分代码,contours(0, 1, 60)返回的是第0个和第2个参数70%的置信区间边界上的60个点的(x,y)坐标,我想将其输出到终端中,但用std::vector<std::pair<double,double> >::iterator 型迭代器竟然不行,所以想请教各位如何读出std::vector<std::pair<double,double> >中的值对?[/b]

[b]主程序部分代码


// create contours factory with FCN and minimum
MnContours contours(fFCN, min);

// 70% confidence level for 2 parameters contour around the minimum
fFCN.SetErrorDef(2.41);
std::vector<std::pair<double,double> > cont = contours(0, 1, 60);

// Output
std::cout<<"contour 70%: "<<std::endl;
std::vector<std::pair<double,double> >::iterator Milkshakeiterator;
for(Milkshakeiterator = cont.begin(); Milkshakeiterator!=cont.end();
++Milkshakeiterator)
{std::cout<<*Milkshakeiterator<<std::endl;
}


错误信息

Data Read In.cpp: In function ‘int main()’:
Data Read In.cpp:168: error: no match for ‘operator<<’ in ‘std::cout <<
Milkshakeiterator.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [wi
th _Iterator = std::pair<double, double>*, _Container = std::vector<std::pair<double, double>,
std::allocator<std::pair<double, double> > >]()’
/usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:108: note: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostrea
m<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT,
_Traits>&)) [with _CharT = char, _Traits = std::char_trai
ts<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:117: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostrea
m<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&))
[with _CharT = char, _Traits = std::char_traits<char>
]
/usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:127: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostrea
m<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:165: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostrea
m<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/lib/gcc/i586-redhat-linux/4.4.0/../../../../include/c++/4.4.0/ostream:169: note:
std::basic_ostream<_CharT, _Traits>& std::basic_ostrea
m<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
...


(PS:简单说明下我的工作背景。我做的工作很简单,就是将理论和实验数据做非线性最小二乘拟合,定出理论公式中的两个参数的值。这里我借用了一个叫做Minuit的求函数最小值的类库,是由欧洲核子中心CERN开发的,是一个求函数最小值和做参数拟合的包。提供的methods可以实现各种操作,例如MnContours类的成员函数contour(0,1,60)就可以把第0个参数和第1个参数的95%置信区间边界的(x,y)坐标,所以返回值是一系列的二维点的(x,y)坐标值对。而我所做的,仅需在程序里include适当的头文件,然后建立相应类的对象,最后调用该对象的成员函数来实现各种操作。)
...全文
756 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
诵经人 2012-11-17
  • 打赏
  • 举报
回复
std::vector<std::pair<double, double> >cont; 申明的时候为什么最后的地方要有个空格呢? 不然就编译不过!
limh821 2010-05-19
  • 打赏
  • 举报
回复
for((i=0;i<60;i++)
{std::cout<<cont[i].first<<","<<cont[i].second<<std::endl;
}
经过验证是行的,xhd3767所说的应该也是可以的,在此谢谢两位热心的帮忙
~CSDN热心的高手就是多,呵呵
xhd3767 2010-05-18
  • 打赏
  • 举报
回复
好久没摸C++了,感觉就是不一样..
xhd3767 2010-05-18
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>

using namespace std;
ostream& operator<<(ostream& os,pair<double,double>& temp)
{
os << temp.first<<" "<<temp.second<<endl;
return os;
}
int main()
{
pair<double,double> t(1,2);
cout <<t<<endl;
return 0;

}
xhd3767 2010-05-18
  • 打赏
  • 举报
回复
可以通过重载 <<实现的
limh821 2010-05-18
  • 打赏
  • 举报
回复
楼上是说改成
for((i=0;i<60;i++)
{std::cout<<cont[i].first<<cont[i].second<<std::endl;
}
吗?谢谢你,我先试试看行不。
pengzhixi 2010-05-18
  • 打赏
  • 举报
回复
或者 Milkshakeiterator->first,Milkshakeiterator->second,
pengzhixi 2010-05-18
  • 打赏
  • 举报
回复
cont[i].first,cont[i].second

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧