- 最後登錄
- 2024-5-8
- 在線時間
- 3180 小時
- 註冊時間
- 2004-8-20
- 閱讀權限
- 20
- 精華
- 0
- UID
- 12056
- 帖子
- 475
- 積分
- 967 點
- 潛水值
- 31699 米
| 回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。 snowflying 發表於 2018-7-5 03:12 AM
strcmp 在 cplusplus.com 有說明喔
return value 那邊 - #include<iostream>
- #include<windows.h>
- #include<string>
- using namespace std;
- const int max_count = 5;
- string str[max_count];
- //隨意填值...反正是測試
- void set_data()
- {
- str[0] = (char)1;
- str[1] = "ABCD";
- str[2] = "~~~~~~~~~~~~~";
- str[3] = "ABCD";
- str[4] = (char)255;
- }
- void logic()
- {
- bool c1 = false;
- for (int i = 0, ii = 0; i < max_count; i++)
- {
- for (int j = i + 1; j < max_count; j++, ii++)
- {
- int ans = strcmp(str[i].c_str(), str[j].c_str());
- if (ans == 0)//代表相同
- {
- c1 = true;
- //cout << "index = " << i << " 的 " << str[i] << " 和 " << "index = " << j << " 的 " << str[j] << " 是一樣的\n";
- //break;//若是抓到一樣就中斷的話....
- }
- cout << "前比較字串index = " << i << "\t" << str[i] << "\t後比較字串index = " << j << "\t" << str[j] << "\t結果 = " << ans << "\n";
- }
- }
- if (c1)//有字串是一樣的
- {
- //動作
- }
- }
- void main()
- {
- set_data();
- logic();
- system("pause");
- }
複製代碼 以下是執行結果
重點是index = 0 and 2 and 4- 前比較字串index = 0 後比較字串index = 1 ABCD 結果 = -1
- 前比較字串index = 0 後比較字串index = 2 ~~~~~~~~~~~~~ 結果 = -1
- 前比較字串index = 0 後比較字串index = 3 ABCD 結果 = -1
- 前比較字串index = 0 後比較字串index = 4 結果 = -1
- 前比較字串index = 1 ABCD 後比較字串index = 2 ~~~~~~~~~~~~~ 結果 = -1
- 前比較字串index = 1 ABCD 後比較字串index = 3 ABCD 結果 = 0
- 前比較字串index = 1 ABCD 後比較字串index = 4 結果 = -1
- 前比較字串index = 2 ~~~~~~~~~~~~~ 後比較字串index = 3 ABCD 結果 = 1
- 前比較字串index = 2 ~~~~~~~~~~~~~ 後比較字串index = 4 結果 = -1
- 前比較字串index = 3 ABCD 後比較字串index = 4 結果 = -1
- 請按任意鍵繼續 . . .
複製代碼 依舊是只有0 1 -1 三種結果...生不出其他結果
為此...我還特地去翻閱我撰寫的其他單晶片程式...不同總類的也就只有3種...我知道不多orz...更甚至linux下的C++...執行亦是如此
當然,我知道糾結這節點很沒意思,而且msdn也是如大眾說明說的的0,>0,<0,反正判斷是否相等就用回傳值是否==0罷了...只是就結果論...我是真的看不到0 1 -1 以外的回傳值就是... |
|