请教STL中for_each的使用

namtaerg 2010-01-18 06:40:11
代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
// STL函数for_each()
using namespace std;
struct Book{
string title;
int price;
};
void showBook(Book &b){
cout << b.title << b.price << endl;
};

int main()
{
Book books[]={{"C Primer Plus",45}, {"C++ Primer Plus",54}};
vector<Book> mybooks;
for (int i=0; i < sizeof(books)/sizeof(Book); i++)
{
mybooks.push_back(books[i]);
};
for_each(mybooks.begin(), mybooks.end(), showBook);// 使用STL函数for_each()
return 0;
}

可编译后出现如下错误提示:

1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>main.obj : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "public: __thiscall std::_Vector_const_iterator<struct Book,class std::allocator<struct Book> >::_Vector_const_iterator<struct Book,class std::allocator<struct Book> >(struct Book *,class std::_Container_base const *)" (??0?$_Vector_const_iterator@UBook@@V?$allocator@UBook@@@std@@@std@@QAE@PAUBook@@PBV_Container_base@1@@Z)
1>libcpmtd.lib(cout.obj) : error LNK2001: unresolved external symbol __CrtDbgReportW
1>libcpmtd.lib(stdthrow.obj) : error LNK2001: unresolved external symbol __CrtDbgReportW
1>libcpmtd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol __CrtDbgReportW
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
1>libcpmtd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol __malloc_dbg
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
1>libcpmtd.lib(xmbtowc.obj) : error LNK2001: unresolved external symbol __free_dbg
1>libcpmtd.lib(_tolower.obj) : error LNK2019: unresolved external symbol __calloc_dbg referenced in function __Getctype
1>.\Debug/Chapter21_1.exe : fatal error LNK1120: 4 unresolved externals


请问大家,如何解决编译问题?或者说,如何使用for_each()?
...全文
154 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
namtaerg 2010-01-18
  • 打赏
  • 举报
回复
原先建立Win32 Console时没有放预编译,特别是你所说的#include "stdafx.h"。
现在重新创一个了一个待预编的,就好了。

呵呵,谢谢大家。

[Quote=引用 6 楼 pengzhixi 的回复:]
加上include "stdafx.h"
[/Quote]
herry-Li 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pengzhixi 的回复:]
加上include "stdafx.h"
[/Quote]

此仁兄所言极是
namtaerg 2010-01-18
  • 打赏
  • 举报
回复
从编译错误提示来看,感觉是缺少了某个库,但就是不知道啊~~~~

VS2005为啥就不能编译通过呢?
pengzhixi 2010-01-18
  • 打赏
  • 举报
回复
加上include "stdafx.h"
herry-Li 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 namtaerg 的回复:]
引用 2 楼 lijiepub 的回复:
楼主用的VC6.0?vs2008下没问题


俺用的是VS2005
[/Quote]


我肯定是你的编译器问题
namtaerg 2010-01-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lijiepub 的回复:]
楼主用的VC6.0?vs2008下没问题
[/Quote]

俺用的是VS2005
clhposs 2010-01-18
  • 打赏
  • 举报
回复
#include <vector>
#include <algorithm>
#include <iostream>

// The function object multiplies an element by a Factor
template <class Type>
class MultValue
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue ( const Type& _Val ) : Factor ( _Val ) {
}

// The function call for the element to be multiplied
void operator ( ) ( Type& elem ) const
{
elem *= Factor;
}
};

// The function object to determine the average
class Average
{
private:
long num; // The number of elements
long sum; // The sum of the elements
public:
// Constructor initializes the value to multiply by
Average ( ) : num ( 0 ) , sum ( 0 )
{
}

// The function call to process the next elment
void operator ( ) ( int elem ) \
{
num++; // Increment the element count
sum += elem; // Add the value to the partial sum
}

// return Average
operator double ( )
{
return static_cast <double> (sum) /
static_cast <double> (num);
}
};

int main( )
{
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;

// Constructing vector v1
int i;
for ( i = -4 ; i <= 2 ; i++ )
{
v1.push_back( i );
}

cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// Using for_each to multiply each element by a Factor
for_each ( v1.begin ( ) , v1.end ( ) , MultValue<int> ( -2 ) );

cout << "Multiplying the elements of the vector v1\n "
<< "by the factor -2 gives:\n v1mod1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// The function object is templatized and so can be
// used again on the elements with a different Factor
for_each (v1.begin ( ) , v1.end ( ) , MultValue<int> (5 ) );

cout << "Multiplying the elements of the vector v1mod\n "
<< "by the factor 5 gives:\n v1mod2 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// The local state of a function object can accumulate
// information about a sequence of actions that the
// return value can make available, here the Average
double avemod2 = for_each ( v1.begin ( ) , v1.end ( ) ,
Average ( ) );
cout << "The average of the elements of v1 is:\n Average ( v1mod2 ) = "
<< avemod2 << "." << endl;
}
herry-Li 2010-01-18
  • 打赏
  • 举报
回复
楼主用的VC6.0?vs2008下没问题
pengzhixi 2010-01-18
  • 打赏
  • 举报
回复
DEV-CPP XP下编译运行都没问题

64,664

社区成员

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

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