簡體   English   中英

'std :: out_of_range'what():basic_string :: substr:__ post

[英]'std::out_of_range' what(): basic_string::substr: __pos

我正在為我的課程編寫一個程序,該程序將按照專輯名稱排序專輯列表,然后按字母順序對專輯歌曲進行排序。 我收到錯誤'std :: out_of_range'what():basic_string :: substr:__ post(is is 4)> this-> size()(為0)。 我的老師只是把它扔給我們而沒有解釋太多,所以我被卡住了。 這是我到目前為止的代碼:

#include <iostream>
#include <string>

using namespace std;

struct album
 {
    string name;
    string release;
    string genre;
    string songs[12];
    int count;
 };

int main()
 {
  album albums[4];
        for (int an = 0; an < 3; an++)
      {
            for (int i = 0; i < albums[an].count-1; i++)
              {

                    if (albums[an].songs[i].substr(4) > albums[an].songs[i+1].substr(4))
                      {
                            swap (albums[an].songs[i], albums[an].songs[i+1]);
                      }
              }
      }

輸入數據是包含發行日期,流派和歌曲的專輯列表:

Walk, Don't Run 
Released 1960
Genre    Instrumental rock, Surf
 1. Morgen
 2. Raunchy
 3. Home
 4. My Own True Love (Tara's Theme)
 5. The Switch
 6. Walk, Don't Run
 7. Night Train
 8. No Trespassing
 9. Caravan
10. Sleepwalk
11. The McCoy
12. Honky Tonk
================================== 
Another Smash!!!  
Released 1961
Genre    Surf rock 
 1. (Ghost) Riders in the Sky (2:28)
 2. Wheels (1:55)
 3. Lonely Heart (2:10)
 4. Bulldog (2:20)
 5. Lullaby of the Leaves (1:58)
 6. Beyond the Reef (3:05)
 7. Raw-Hide (2:29)
 8. Meet Mister Callahan (2:20)
 9. Trambone (2:04)
10. Last Date (2:13)
11. Ginchy (1:40)
12. Josie (2:04)
================================== 
The Ventures Play Telstar and the Lonely Bull
Released 1963
Genre    Surf rock 
 1. Telstar (2:37)
 2. The Lonely Bull (2:11)
 3. Mexico (2:26)
 4. Calcutta (2:20)
 5. Apache (3:08)
 6. Never on Sunday (2:14)
 7. Tequila (2:44)
 8. Green Onions (2:05)
 9. Percolator (2:14)
10. Red River Rock (2:15)
11. Let There Be Drums (2:20)
12. Last Night (2:29)
================================== 
Hawaii Five-O 
Released 1969
Genre    Instrumental
 1. Hawaii Five-O (1:59)
 2. Lovin' Things (2:31)
 3. Galveston (2:40)
 4. The Letter (2:10)
 5. Don't Give in to Him (2:12)
 6. Theme from A Summer Place (2:16)
 7. Medley: Spooky/Traces/Stormy (4:25)
 8. Medley: Aquarius/Let the Sunshine In (2:49)
 9. Games People Play (2:46)
10. I Can Hear Music (2:37)
11. Dizzy (2:31)

這里的問題是你的substr()調用。 看起來您傳遞的substr()參數(要復制為子字符串的第一個字符的位置)大於字符串長度。

如果此參數大於字符串長度,則只會拋出out_of_range。 您在代碼中獲得的異常。

為避免這種情況,請確保將有效值傳遞給substr()調用。

此外,您應該在執行此排序/交換操作之前首先接受輸入數據,否則程序可能會在拋出異常后終止。

對於前者 你的代碼需要if語句才能正常工作。 if你沒有提供任何值,那么它將獲取像4195901這樣的垃圾值, if語句將在i達到12時拋出以下異常(因為歌曲是12的數組)你的代碼中的字符串):

在拋出'std :: length_error'的實例后調用終止what():basic_string :: _ S_create Aborted

我希望這能幫到您。

問題出在歌曲Home of the Walk,Do not Run

Walk, Don't Run 
Released 1960
Genre    Instrumental rock, Surf
1. Morgen
2. Raunchy
3. Home //In this line song length is 4 ie. 0 to 3 
        //if you do substr(4) then it will throws 'std::out_of_range'.
        //So check the length of the song before substr().

暫無
暫無

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

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