[Warning] comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'unsigned int'} [-Wsign-compare]
时间: 2024-01-23 21:03:38 浏览: 213
这个警告的意思是在比较有符号整型和无符号整型时最好避免。在你的代码中,比较的是一个int类型和一个size_t类型(也就是unsigned int类型)。这样的比较会导致警告的产生。\[1\]\[2\]
为了解决这个问题,你可以将循环变量i的类型改为无符号整型,即将int改为unsigned int。这样就可以避免警告的产生。\[3\]例如,你可以这样修改代码:
for (unsigned int i = 0; i != histogram.size(); ++i) {
total += histogram\[i\];
}
这样修改后,警告就不会再出现了。
#### 引用[.reference_title]
- *1* [effective c++ 53-不要忽略编译器的警告](https://blog.csdn.net/qq_31442743/article/details/130860738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [C++学习中遇到的警告comparison between signed and unsigned integer expressions [-Wsign-compare]](https://blog.csdn.net/qq_43792862/article/details/97647454)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [警告:comparison between signed and unsigned integer expressions [-Wsign-compare]](https://blog.csdn.net/weixin_43638873/article/details/115793615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文