cout输出乱码的问题
刚写的小程序,其他地方没有问题,但是在print()方法里如果用cout就输出的是乱码,用printf正常,不理解,请达人指点
#include <iostream>
class HeapSort{
private:
int* array;
int length;
protected:
void swap(int x,int y){
int temp = array[x];
array[x]=array[y];
array[y]=temp;
}
void adjust(int index,int endIndex){
while((2*index+1) <=endIndex){
int nextIndex;
if((2*index+2)> endIndex){
nextIndex = 2*index+1;
}
else{
nextIndex = array[2*index+1]> array[2*index+2]?(2*index+1):(2*index+2);
}
if(array[index] <array[nextIndex]){
swap(index,nextIndex);
index = nextIndex;
}else{
break;
}
}
}
public:
HeapSort(int* array,int length):array(array),length(length){}
void print(){
for(int i = 0 ; i < length; i++){
//std::cout < <array[i] < <std::cout < < " ";//为什么这样写输出是乱码
printf( "%d ",array[i]);
}
std::cout < <std::endl;
}
void biuldHeap(){
for(int index = (length-1)/2;index> =0;index--){
adjust(index,length-1);
}
}
void sort(){
for(int endIndex = length - 1 ;endIndex> =0;endIndex--){
swap(0,endIndex);
adjust(0,endIndex-1);
print();
}
}
};
void main(){
int array[10]={5,4,3,2,1,6,7,8,9,10};
HeapSort test(array,10);
test.print();
test.biuldHeap();
test.print();
test.sort();
test.print();
}
[解决办法]
std::cout < <array[i] < <std::cout < < " ";
大哥, 你要把 std::cout 也输出出去啊.
要这样哦.
std::cout < < array[i] < < " ";