希望我能就我制作的排序方法得到一些建议.
这只是我正在制作的另一个程序的测试,这个测试有一个我无法弄清楚的错误.此代码的目的是创建一个int指针数组,并通过regular int数组的内容对该数组中的指针进行排序.
该错误是我的第二个for循环,它不允许我使用aj!= - 1因此不允许我对数组的第一个元素进行排序.请帮忙.谢谢!!
//create array
int c[8] = {3,1,5,7,8,2,6,4};
//create pointer array
int *newptr[8];
for(int k = 0; k<8; k++)
{
newptr[k] = &c[k];
}
//sort pointer array
for(int j = 0; j<8; j++)
{
cout << "test1\n\n";
cout << *newptr[j] << "and" << *newptr[j+1];
for(;*newptr[j] < *newptr[j+1] && j!=0; j--)
//using j!=-1 doesn't work which causes me to not be able to sort the first element
//in the array properly
{
cout<< "test2";
int *temp;
temp …Run Code Online (Sandbox Code Playgroud) 希望我能就我制作的排序方法得到一些建议.
此代码的目的是创建一个int指针数组,并通过regular int数组的内容对该数组中的指针进行排序.然后根据原始int数组的位置为不同的变量赋值.
我在使用这段代码时遇到的奇怪之处在于测试代码在我知道的情况下不应该影响任何东西......实际上是影响了我指针的内容.也许值不会改变,但我编写测试代码的方式会导致错误.
//create array
int c[8] = {3,1,5,7,8,2,6,4};
//create pointer array
int *newptr[8];
for(int k = 0; k<8; k++)
{
newptr[k] = &c[k];
}
//sort pointer array
for(int j = 0; j<8; j++)
{
for(; j > -1 && *newptr[j] < *newptr[j+1]; j--)
{
int *temp = newptr[j+1];
newptr[j+1] = newptr[j];
newptr[j] = temp;
}
}
//set lookuplocation
int lookuplocation;
for(int i = 0; i<8; i++)
{
cout << *newptr[i];
if(newptr[i] == &c[0])
{
cout << *newptr[i] << …Run Code Online (Sandbox Code Playgroud) 我想对一系列日期进行排序.
示例格式:"2017-11-13_07-55-40"或年 - 月 - 日_时 - 分 - 秒
array={"2017-11-13_09-55-42" "2017-11-13_08-30-40" "2017-11-13_07-55-40"}
Run Code Online (Sandbox Code Playgroud)