問題タブ [string-view]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - std::string_view を int に安全に変換 (stoi や atoi など)
に変換する安全な標準的な方法はありますか?std::string_view
int
C++11では次のように変換std::string
できます:stoi
int
ただしstoi
、サポートしていませんstd::string_view
。別の方法として、 を使用することもできますatoi
が、次のように非常に注意する必要があります。
atoi
nullターミネータに基づいているため'\0'
(たとえば、sv.substr
単純に挿入/追加できないため)、どちらも機能しません。
現在、C++17 以降には もありますがfrom_chars
、不十分な入力を提供するとスローされないようです: