'std::out_of_range' what(): basic_string::substr: __
'std::out_of_range' what(): basic_string::substr: __pos
我正在为我的类编写一个程序,该程序将按专辑名称对专辑列表进行排序,然后按字母顺序对专辑歌曲进行排序。我收到错误"std::out_of_range"what((:basic_string::substr:__pos(即 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
count 的值才能正常工作。如果您不提供任何值,那么它将采用垃圾值,如 4195901
并且当i
到达12
时if
语句将抛出以下异常(因为 songs 是代码中的12
字符串数组(:
在抛出"std::length_error"实例后终止调用 什么((: basic_string::_S_create 中止
我希望这对你有帮助。
问题出在专辑的家走,不要跑
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().
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量