gpt4 book ai didi

c++ - 如何选择字符串中的特定字符?

转载 作者:行者123 更新时间:2023-12-02 10:12:31 26 4
gpt4 key购买 nike

我对C++中的字符串操作非常陌生,而且似乎无法将精力集中在如何选择字符串中的字符上。以下是我到目前为止的程序。但是,无论何时运行它,程序都会崩溃,并显示错误消息“Thread 1:EXC_BAD ...”(我正在使用Xcode)。出于某种原因,如果我想从字符串中打印第一个字符,仅使用input [0]即可,但是当我循环执行input [x]时则无法使用。

#include <iostream>
#include <string>

int num, characterCount;
string currentChar, previousChar;

int main()
{
cout << "How many lines of input? \n";
cin >> num;
for (int x = 0; x < num; x++)
{
string input;
cin >> input;

previousChar = input[0];
currentChar = input[1];
characterCount = 0;
int y = 0;
while (currentChar != "")
{
y++;
if (previousChar == currentChar)
{
characterCount++;
}
else if (previousChar != currentchar && currentChar != "")
{
cout << characterCount << " " << previousChar;
characterCount = 0;
}
else if (currentChar == "")
{
cout << characterCount << " " << previousChar;
}
previousChar = currentChar;
currentChar = input[y];
}
}
return 0;
}
顺便说一句,我知道我的代码很奇怪,我是C++的初学者。如果您有任何有用的建议,请随时发表评论!

最佳答案

您收到错误的原因是:

currentChar = input[y];
这行的问题是 input[y]可能无法访问,例如您的 input"Hello"。用您编写代码的方式, y最终将等于 5。但是,最后一个字母 'o'input[4]。因此,当 y=5时,您将得到一个错误。
有几种方法可以修复它。一个已经提到使用 .size()来确保,另一个已经提到使用迭代器: for(auto iterator = input.begin();iterator!=input.end();++iterator)但是,我要提到的是,您应该将 currentCharpreviousChar声明为 char类型而不是 string,因为它们始终是单个字符。另外,取决于您使用的C++版本和编译器,如果您使用的是C++ 11,则 input[5]上的 "Hello"实际上很好。
当您将 input[5]传递给 char currentChar类型时, currentChar获得一个空字符。因此,对于while循环,您可以测试 while(currentChar)。当 currentChar获得空字符时,while循环将测试为false,并结束循环。
以下是我在您的for循环内的更改方式。它们中的大多数保持不变,除了将 previousCharcurrentChar更改为 char类型,并对它们进行一些条件检查之外,还进行了一些小的更改以完善它们。
#include <iostream>
#include <string>

int main() {
int num;
std::cout << "How many lines of input? \n";
std::cin >> num;
for (int x = 0; x < num; x++)
{
std::string input;
std::cin >> input;

char previousChar = input[0];
int y = 1;
char currentChar = input[y];
int characterCount = 1;
while(currentChar)
{
y++;
if (previousChar == currentChar)
{
characterCount++;
}
else
{
std::cout << characterCount << " " << previousChar << "\n";
characterCount = 1;
}
previousChar = currentChar;
currentChar = input[y];
}
std::cout << characterCount << " " << previousChar << "\n";
}
}

还要注意,在为我遍历索引号时使用while循环感觉很奇怪。因此,我可能会使用for循环而不是while循环:
#include <iostream>
#include <string>

int main() {
int num;
std::cout << "How many lines of input? \n";
std::cin >> num;
for (int x = 0; x < num; x++)
{
std::string input;
std::cin >> input;

char previousChar = input[0];
int y = 1;
char currentChar = input[y];
int characterCount = 0;
for(int y = 1; currentChar; y++)
{
if (previousChar == currentChar)
{
characterCount++;
}
else
{
std::cout << characterCount << " " << previousChar << "\n";
characterCount = 1;
}
previousChar = currentChar;
currentChar = input[y];
}
std::cout << characterCount << " " << previousChar << "\n";
}
}

这是使用迭代器的方法:
#include <iostream>
#include <string>

int main() {
int num;
std::cout << "How many lines of input? \n";
std::cin >> num;
for (int x = 0; x < num; x++)
{
std::string input;
std::cin >> input;

char previousChar, currentChar;
previousChar = input[0];
int characterCount = 1;
for(auto it = input.begin()+1; it != input.end();it++)
{
currentChar = *it;
if (previousChar == currentChar)
{
characterCount++;
}
else
{
std::cout << characterCount << " " << previousChar << "\n";
characterCount = 1;
}
previousChar = currentChar;
}
std::cout << characterCount << " " << previousChar << "\n";
}
}

关于c++ - 如何选择字符串中的特定字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62986705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com