char*在BCB和VC中有什么区别
#include "stdafx.h "
#include <vector>
#include <algorithm>
#include <iostream.h>
#include <list>
using namespace std;
PrintIt(char*& StringToPrint)
{
cout < <StringToPrint < <endl;
}
int main(int argc, char* argv[])
{
list <char*> staff;
list <char*> ::iterator people;
staff.push_back( "John ");
staff.push_back( "Bill ");
staff.push_back( "Tony ");
staff.push_back( "Fidel ");
staff.push_back( "Nelson ");
cout < < "The unsorted vector " < <endl;
for_each(staff.begin(),staff.end(),PrintIt);
staff.sort();
cout < < "The Sorted vector " < <endl;
for_each(staff.begin(),staff.end(),PrintIt);
return 0;
}
以上程序我在BCB中运行不会排序,而在VC中运行会排序,请教大家是什么原因,并且在BCB中CHAR*好像不起到什么做用,
谁能说一下CHAR*和STRING的区别吗?
[解决办法]
char*是指针
string是class
[解决办法]
因为是对指针的地址进行排序的,所以,排序结果未知
[解决办法]
刚才说错了 是
因为是对指针的大小进行排序的,所以,排序结果未知
[解决办法]
楼主人品还不是一般的好,竟然在VC下,根据地址值大小排序还得到了自己想要的结果。
呃,用string取代char *吧。它能估计字符串内容进行比较。
现在应该任何一本合格的C++书都会解决string的了。
[解决办法]
这样的操作是对 char * 这个地址值进行排序 ...
char * 改成 string吧 ...
就不会有问题了。
使用string的话,
排序是基于 string 内容的。
[解决办法]
#include <algorithm>
#include <list>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
void PrintIt(string StringToPrint)
{
cout < <StringToPrint < <endl;
}
int main(int argc, char* argv[])
{
list <string> staff;
list <char*> ::iterator people;
staff.push_back( "John ");
staff.push_back( "Bill ");
staff.push_back( "Tony ");
staff.push_back( "Fidel ");
staff.push_back( "Nelson ");
cout < < "The unsorted vector " < <endl;
for_each(staff.begin(),staff.end(),PrintIt);
staff.sort();
cout < < "The Sorted vector " < <endl;
for_each(staff.begin(),staff.end(),PrintIt);
system( "pause ");
return 0;
}
[解决办法]
bool lit(char* _l, char* _r)
{
if (strcmp(_l, _r) == -1)
return true;
return false;
}
void PrintIt(char*& StringToPrint)
{
cout < <StringToPrint < <endl;
}
int main(int argc, char* argv[])
{
list <char*> staff;
list <char*> ::iterator people;
staff.push_back( "John ");
staff.push_back( "Bill ");
staff.push_back( "Tony ");
staff.push_back( "Fidel ");
staff.push_back( "Nelson ");
cout < < "The unsorted vector " < <endl;
for_each(staff.begin(),staff.end(),PrintIt);
staff.sort(lit);
cout < < "The Sorted vector " < <endl;
for_each(staff.begin(),staff.end(),PrintIt);
return 0;
}
[解决办法]
没有用过char * 排序!
[解决办法]
你得自己为char *写一个比较仿函数
否则sort排序是会根据地址来比较
在VS下,会排序,可能是LZ近来RP值 好:-)
[解决办法]
对指针的大小进行排序的,所以,排序结果未知