簡體   English   中英

std :: string構造函數拋出std :: out_of_range異常

[英]std::string constructor throws std::out_of_range exception

使用VS 2012。

我正在制作劊子手。 無論如何,我有一個函數來獲取一個std :: string,其長度與猜測的當前單詞的長度相同,但是用下划線填充。 (如,空白)。

功能:

std::string getBlankWord(std::vector<std::string> words, int currentWordIndex)
{
    return std::string(words[currentWordIndex], '_');
}

它被調用的行:

currentGuessingString = getBlankWord(words, index);

單詞是std :: vector,index是int。 words.size()= 22,index = 0,所以我不知道如何調用,在這種情況下,單詞[0]可能是罪魁禍首。 無論如何,這一行上的某些內容會拋出std :: out_of_range異常,但我無法找到它。

謝謝。

我覺得你真正想要的更像是:

return std::string(words[currentWordIndex].size(), '_');

出現問題是因為最終被調用的構造函數是子字符串構造函數(參見此處

第一個參數很容易弄明白,但第二個參數將是一個char,其ascii值為'_'隱式地轉換為size_t,其'值很可能大於你給構造函數的字符串的大小這反過來會導致你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM