首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这是小弟我在钱能的书上照抄下来的,为什么不对

2012-03-15 
这是我在钱能的书上照抄下来的,为什么不对?而且它自己提供的源代码运行也出错,请各位大哥帮忙解决一下。另

这是我在钱能的书上照抄下来的,为什么不对?
而且它自己提供的源代码运行也出错,请各位大哥帮忙解决一下。
另外谁能给我讲讲里面那个replace()的用法及含义啊?
自己刚刚自学,请各位前辈指教啊!
#include <iostream>

#include <algorithm>
using   namespace   std;
void   main()
{
string   a,s1= "hello ";
string   s2= "123 ";
cout < <(a==s1   ?   " "   :   "not ") < < "equal\n ";
cout < <a+s2 < <endl;
reverse(a.begin(),a.end());
cout < <a < <endl;
cout < <a.replace(0,9,9, 'c ');
cout < <(s1.find( "ell ")!=-1? " ": "not ") < < "found\n ";
cout < <(s1.find( 'c ')!=-1   ?   " "   :   "not ") < < "found\n ";
}

[解决办法]


#include <iostream>
#inlcude <string> -----------------------add this.
#include <algorithm>
using namespace std;
void main()
{
string a,s1= "hello ";
string s2= "123 ";
cout < <(a==s1 ? " " : "not ") < < "equal\n ";
cout < <a+s2 < <endl;
reverse(a.begin(),a.end());
cout < <a < <endl;
cout < <a.replace(0,9,9, 'c ');
cout < <(s1.find( "ell ")!=-1? " ": "not ") < < "found\n ";
cout < <(s1.find( 'c ')!=-1 ? " " : "not ") < < "found\n ";
}

[解决办法]
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void main()
{
string a,s1= "hello ";
string s2= "123 ";
cout < <(a==s1 ? " " : "not ") < < "equal\n ";
cout < <a+s2 < <endl;
reverse(a.begin(),a.end());
cout < <a < <endl;
cout < <a.replace(0,9,9, 'c ');
cout < <(s1.find( "ell ")!=-1? " ": "not ") < < "found\n ";
cout < <(s1.find( 'c ')!=-1 ? " " : "not ") < < "found\n ";
}

[解决办法]
Examines each element in a range and replaces it if it matches a specified value.
template <class ForwardIterator, class Type>
void replace(
ForwardIterator _First,
ForwardIterator _Last,
const Type& _OldVal,
const Type& _NewVal
);
Parameters
_First
A forward iterator pointing to the position of the first element in the range from which elements are being replaced.
_Last
A forward iterator pointing to the position one past the final element in the range from which elements are being replaced.
_OldVal
The old value of the elements being replaced.
_NewVal
The new value being assigned to the elements with the old value.
Remarks
The range referenced must be valid; all pointers must be dereferenceable and within the sequence the last position is reachable from the first by incrementation.
The order of the elements not replaced remains stable.
The operator== used to determine the equality between elements must impose an equivalence relation between its operands.
The complexity is linear; there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.


Example
// alg_replace.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

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

int i;
for ( i = 0 ; i <= 9 ; i++ )
{
v1.push_back( i );
}

int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
{
v1.push_back( 7 );
}

random_shuffle (v1.begin( ), v1.end( ) );
cout < < "The original vector v1 is:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout < < *Iter1 < < " ";
cout < < "). " < < endl;

// Replace elements with a value of 7 with a value of 700
replace (v1.begin( ), v1.end( ), 7 , 700);

cout < < "The vector v1 with a value 700 replacing that of 7 is:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout < < *Iter1 < < " ";
cout < < "). " < < endl;
}
Output
The original vector v1 is:
( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 ).
The vector v1 with a value 700 replacing that of 7 is:
( 700 1 9 2 0 700 700 3 4 6 8 5 700 700 ).

热点排行