0

わかりました、それはばかげた質問かもしれませんが、とにかく質問を続けます。

それで、 std::string の使用に関連する可能性のあるすべてのエラーは何ですか? たとえば、さまざまな std::string 関数で std::string サイズよりも大きい場所で char にアクセスするなど、いくつか知っています。

プログラミング中にどのエラーを念頭に置いてチェックする必要がありますか?

たとえば、効率的に次のことを行う別の方法はありますか?

std::string s("some string.");

int i = s.find ( "." );


if (  i != std::string::npos    &&  i + 3 < s.length (  )  ) // <<== this check is what I am talking about
    s.erase ( i + 3 );

私は何百ものそのようなチェックを必要とするプログラムを持っているので、毎回 if( some_condition ) を行う別の方法があると思っていました。

4

2 に答える 2

0

i文字列の長さより大きい場合、例外out_of_rangeがスローされます

参照:- std::string::erase

だからいつでも釣れる!

std::string s("some string.");
int i = s.find ( "." );

if (i != std::string::npos)
try 
{
   s.erase ( i + 3 );
}
catch (const std::out_of_range& err) 
{
  std::cerr << "Out of Range error: " << err.what() << std::endl;
}
于 2013-08-12T08:06:13.503 に答える