C++中所赋的值即是赋值表达式的结果
在看数据结构的时候看到冒泡排序一种奇怪的写法,不明所以,被循环条件弄得很懵,才想起来这个结论:
#include <iostream> using namespace std; void swap(int &a, int &b) { int temp = b; b = a; a = temp; } void bubblesort(int A[], int n) { int t = n; for (bool sorted = false; sorted = !sorted; n--) { for (int i = 1; i < n; ++i) { if (A[i - 1] > A[i]) { swap(A[i - 1], A[i]); sorted = false; } } } for (int j = 0; j < t; ++j) { cout << A[j] << " "; } } int main() { // int A[] = {5, 6, 7, 4, 2, 8}; int A[] = {2, 4, 5, 6, 7, 8}; bubblesort(A, sizeof(A) / sizeof(int)); return 0; }