C++ substr() 方法在使用时似乎 return 0
C++ substr() method seems to return 0 when used
我正在编写一个程序,它将读取 5 张 Buddy Holly 专辑的文件,按字母顺序对它们进行排序,按字母顺序对每张专辑中的歌曲进行排序,然后打印专辑名称、发行年份和排序歌曲列表。这里要注意的是,我应该将数字保留在歌曲旁边,所以它会这样显示:
Buddy Holly
1958
10. Baby I Don't Care
7. Everyday
1. I'm Gonna Love You Too
4. Listen to Me
12. Little Baby
3. Look at Me
8. Mailman, Bring Me No More Blues
2. Peggy Sue
11. Rave On!
6. Ready Teddy
5. Valley of Tears
9. Words of Love
当我尝试对歌曲进行排序时出现问题。我的教授建议我使用 substr()
方法跳过字符串中的前 4 个字符以从第一个字母开始比较,但是当我使用它时,它一直给我这个错误:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 4) > this->size() (which is 0)
我回去尝试了 .size()/length()
方法来查看我是否得到了正值(我做了),我还设置了一个等于该值的 int,然后尝试了一个 if 语句来查看该 int 是否大于 0。它不是。
老实说,我在这里不知所措,这是我完成这件事需要做的最后一件事。
我的代码:
#include <iostream>
#include <string>
#include <string.h>
#include <istream>
#include <stdio.h>
#define a 5
using namespace std;
struct buddyH
{
string albumName;
string year;
string songs[12];
int count;
};
int main()
{
buddyH bh[a], thd;
string st, jam;
bool change = true, change2 = true;
for(int i = 0; i < a; ++i)
{ //start read-in for loop
int k = 0;
getline(cin, bh[i].albumName);
getline(cin, bh[i].year);
while (getline(cin, st))
{
if (st[0] == '=') break;
bh[i].songs[k] = st;
k++;
}
bh[i].count = k;
}//end read-in for loop
while(change)
{
change = false;
for(int j = 0; j < a-1; j++)
{
if(bh[j].albumName > bh[j+1].albumName)
{
thd = bh[j];
bh[j] = bh[j+1];
bh[j+1] = thd;
change = true;
}
}
}
while(change2)
{
int chk1, chk2;
change2 = false;
for(int x = 0; x < a; x++)
{
int s = sizeof(bh[x].songs)/sizeof(bh[x].songs[0]);
for(int y = 0; y < s-1; y++)
{
if(bh[x].songs[y].substr(4) > bh[x].songs[y+1].substr(4))
{
jam = bh[x].songs[y];
bh[x].songs[y] = bh[x].songs[y+1];
bh[x].songs[y+1] = jam;
change2 = true;
}
}
}
}
for(int c = 0; c < a; c++)
{
cout << bh[c].albumName << endl;
cout << bh[c].year << endl;
for(int i = 0; i < 12; i++)
{
cout << bh[c].songs[i] << endl;
}
cout << endl;
}
}
输入文件:
That'll Be the Day
1958
1. You Are My One Desire
2. Blue Days, Black Nights
3. Modern Don Juan
4. Rock Around With Ollie Vee
5. Ting A Ling
6. Girl On My Mind
7. That'll Be the Day
8. Love Me
9. I'm Changing All Those Changes
10. Don't Come Back Knockin'
11. Midnight Shift
========================
Buddy Holly
1958
1. I'm Gonna Love You Too
2. Peggy Sue
3. Look at Me
4. Listen to Me
5. Valley of Tears
6. Ready Teddy
7. Everyday
8. Mailman, Bring Me No More Blues
9. Words of Love
10. Baby I Don't Care
11. Rave On!
12. Little Baby
========================
The Buddy Holly Story
1959
1. Raining In My Heart
2. Early in the Morning
3. Peggy Sue
4. Maybe Baby
5. Everyday
6. Rave On!
7. That'll Be the Day
8. Heartbeat
9. Think It Over
10. Oh, Boy!
11. It's So Easy!
12. It Doesn't Matter Anymore
========================
The Buddy Holly Story, Vol. 2
1960
1. Peggy Sue Got Married
2. Well, All Right
3. What to Do
4. That Makes It Tough
5. Now We're One
6. Take Your Time
7. Crying, Waiting, Hoping
8. True Love Ways
9. Learning the Game
10. Little Baby
11. Moondreams
12. That's What They Say
========================
Reminiscing
1963
1. Reminiscing
2. Slippin' and Slidin'
3. Bo Diddley
4. Wait 'Till the Sun Shines, Nellie
5. Baby, Won't You Come Out Tonight
6. Brown Eyed Handsome Man
7. Because I Love You
8. It's Not My Fault
9. I'm Gonna Set My Foot Down
10. Changing All Those Changes
11. Rock-A-Bye Rock
我想你访问的歌曲数组里面没有元素(空字符串)。
请问这行的目的是什么?
int s = sizeof(bh[x].songs)/sizeof(bh[x].songs[0]);
如果您要查找歌曲总数,请将该行替换为以下行:
int s = bh[x].count;
我试过你的代码,它有效。您的程序已成功按照您的描述对歌曲名称进行排序。
您的程序在 substr(4) 空字符串时崩溃,因为歌曲数组中的字符串并未全部填充。您的某些专辑仅包含 11 首歌曲。
我正在编写一个程序,它将读取 5 张 Buddy Holly 专辑的文件,按字母顺序对它们进行排序,按字母顺序对每张专辑中的歌曲进行排序,然后打印专辑名称、发行年份和排序歌曲列表。这里要注意的是,我应该将数字保留在歌曲旁边,所以它会这样显示:
Buddy Holly
1958
10. Baby I Don't Care
7. Everyday
1. I'm Gonna Love You Too
4. Listen to Me
12. Little Baby
3. Look at Me
8. Mailman, Bring Me No More Blues
2. Peggy Sue
11. Rave On!
6. Ready Teddy
5. Valley of Tears
9. Words of Love
当我尝试对歌曲进行排序时出现问题。我的教授建议我使用 substr()
方法跳过字符串中的前 4 个字符以从第一个字母开始比较,但是当我使用它时,它一直给我这个错误:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 4) > this->size() (which is 0)
我回去尝试了 .size()/length()
方法来查看我是否得到了正值(我做了),我还设置了一个等于该值的 int,然后尝试了一个 if 语句来查看该 int 是否大于 0。它不是。
老实说,我在这里不知所措,这是我完成这件事需要做的最后一件事。
我的代码:
#include <iostream>
#include <string>
#include <string.h>
#include <istream>
#include <stdio.h>
#define a 5
using namespace std;
struct buddyH
{
string albumName;
string year;
string songs[12];
int count;
};
int main()
{
buddyH bh[a], thd;
string st, jam;
bool change = true, change2 = true;
for(int i = 0; i < a; ++i)
{ //start read-in for loop
int k = 0;
getline(cin, bh[i].albumName);
getline(cin, bh[i].year);
while (getline(cin, st))
{
if (st[0] == '=') break;
bh[i].songs[k] = st;
k++;
}
bh[i].count = k;
}//end read-in for loop
while(change)
{
change = false;
for(int j = 0; j < a-1; j++)
{
if(bh[j].albumName > bh[j+1].albumName)
{
thd = bh[j];
bh[j] = bh[j+1];
bh[j+1] = thd;
change = true;
}
}
}
while(change2)
{
int chk1, chk2;
change2 = false;
for(int x = 0; x < a; x++)
{
int s = sizeof(bh[x].songs)/sizeof(bh[x].songs[0]);
for(int y = 0; y < s-1; y++)
{
if(bh[x].songs[y].substr(4) > bh[x].songs[y+1].substr(4))
{
jam = bh[x].songs[y];
bh[x].songs[y] = bh[x].songs[y+1];
bh[x].songs[y+1] = jam;
change2 = true;
}
}
}
}
for(int c = 0; c < a; c++)
{
cout << bh[c].albumName << endl;
cout << bh[c].year << endl;
for(int i = 0; i < 12; i++)
{
cout << bh[c].songs[i] << endl;
}
cout << endl;
}
}
输入文件:
That'll Be the Day
1958
1. You Are My One Desire
2. Blue Days, Black Nights
3. Modern Don Juan
4. Rock Around With Ollie Vee
5. Ting A Ling
6. Girl On My Mind
7. That'll Be the Day
8. Love Me
9. I'm Changing All Those Changes
10. Don't Come Back Knockin'
11. Midnight Shift
========================
Buddy Holly
1958
1. I'm Gonna Love You Too
2. Peggy Sue
3. Look at Me
4. Listen to Me
5. Valley of Tears
6. Ready Teddy
7. Everyday
8. Mailman, Bring Me No More Blues
9. Words of Love
10. Baby I Don't Care
11. Rave On!
12. Little Baby
========================
The Buddy Holly Story
1959
1. Raining In My Heart
2. Early in the Morning
3. Peggy Sue
4. Maybe Baby
5. Everyday
6. Rave On!
7. That'll Be the Day
8. Heartbeat
9. Think It Over
10. Oh, Boy!
11. It's So Easy!
12. It Doesn't Matter Anymore
========================
The Buddy Holly Story, Vol. 2
1960
1. Peggy Sue Got Married
2. Well, All Right
3. What to Do
4. That Makes It Tough
5. Now We're One
6. Take Your Time
7. Crying, Waiting, Hoping
8. True Love Ways
9. Learning the Game
10. Little Baby
11. Moondreams
12. That's What They Say
========================
Reminiscing
1963
1. Reminiscing
2. Slippin' and Slidin'
3. Bo Diddley
4. Wait 'Till the Sun Shines, Nellie
5. Baby, Won't You Come Out Tonight
6. Brown Eyed Handsome Man
7. Because I Love You
8. It's Not My Fault
9. I'm Gonna Set My Foot Down
10. Changing All Those Changes
11. Rock-A-Bye Rock
我想你访问的歌曲数组里面没有元素(空字符串)。
请问这行的目的是什么?
int s = sizeof(bh[x].songs)/sizeof(bh[x].songs[0]);
如果您要查找歌曲总数,请将该行替换为以下行:
int s = bh[x].count;
我试过你的代码,它有效。您的程序已成功按照您的描述对歌曲名称进行排序。
您的程序在 substr(4) 空字符串时崩溃,因为歌曲数组中的字符串并未全部填充。您的某些专辑仅包含 11 首歌曲。