avatar
到家了,报个平安# pets - 心有所宠
w*s
1
If the string is not the same length, my code will crash because of array
index out of boundary. Can you help find the bug?
//Refer Algorithm 4th, page 722
void exch(string &a, string &b)
{
string c = a;
a = b;
b = c;
}
int compare(int a, int b)
{
if (aif (a==b) return 0;
if (a>b) return 1;
}
void ThreeWayQsort(vector &v, int low, int high, int d)
{
if (high<=low) return;
//3 way partition to handle duplicate
int lt = low;
int gt = high;
int i = low + 1;
int pivot = v[low][d];
while (i <= gt)
{
if (d>= v[i].size()) {exch(v[i++],v[lt++]);continue;}
int cmp = compare(v[i][d], pivot);
if (cmp < 0) {
exch(v[i++],v[lt++]);
}else if (cmp > 0 ){
exch(v[i],v[gt--]);
}else{
i++;
}
}
// now divide and conqured.
ThreeWayQsort(v,low,lt-1,d);
if (pivot>0) { ThreeWayQsort(v,lt,gt,d+1);}; //recursive here.
ThreeWayQsort(v,gt+1,high,d);
}
void sort(vector &v)
{
int len = v.size();
ThreeWayQsort(v, 0, len-1, 0);
}
void printV(vector &v, string str)
{
cout << str <for (int i = 0; i{
cout << i << ":" << v[i] << endl;
}
}
void ThreeWaySortTestMain()
{
vector v;
v.push_back("she");
v.push_back("sells");
v.push_back("seashells");
v.push_back("by");
v.push_back("the");
v.push_back("sea");
v.push_back("shore");
v.push_back("the");
v.push_back("shells");
v.push_back("she");
v.push_back("sells");
v.push_back("are");
v.push_back("surely");
v.push_back("seashells");

printV(v, "before sort");
sort(v);
printV(v,"after sort");
}
avatar
a*y
2
顺利到家了,看得出俩猫很激动,估计是想我了,走到哪儿跟到哪儿~~
谢谢版上的祝福,围脖和小咪都没瘦,据说在家很乖,除了把能桌上的东西都掀到地上
之外,没有其它的破坏~~虽然医生说我最多只能拿5磅的东西,偶还是没忍住抱了快15
磅的围脖~
祝Pets版每天都有宝贝们的PP照片涌现~~
avatar
w*s
3
貌似与编译器有关。在G++下面运行没有问题,在VC下报异常。

【在 w*******s 的大作中提到】
: If the string is not the same length, my code will crash because of array
: index out of boundary. Can you help find the bug?
: //Refer Algorithm 4th, page 722
: void exch(string &a, string &b)
: {
: string c = a;
: a = b;
: b = c;
: }
: int compare(int a, int b)

avatar
o*l
4
hugs
take good care of yourself
hug them while sitting is okay. =)

15

【在 a******y 的大作中提到】
: 顺利到家了,看得出俩猫很激动,估计是想我了,走到哪儿跟到哪儿~~
: 谢谢版上的祝福,围脖和小咪都没瘦,据说在家很乖,除了把能桌上的东西都掀到地上
: 之外,没有其它的破坏~~虽然医生说我最多只能拿5磅的东西,偶还是没忍住抱了快15
: 磅的围脖~
: 祝Pets版每天都有宝贝们的PP照片涌现~~

avatar
v*a
5

Maybe here:
int pivot = 0;
if (d < v[low].length())
pivot = v[low][d];

【在 w*******s 的大作中提到】
: If the string is not the same length, my code will crash because of array
: index out of boundary. Can you help find the bug?
: //Refer Algorithm 4th, page 722
: void exch(string &a, string &b)
: {
: string c = a;
: a = b;
: b = c;
: }
: int compare(int a, int b)

avatar
u*y
6
bless~
MM好好保养身体~
avatar
y*u
7
Bless!
avatar
C*W
8
blessssssss~~~~
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。