你能弄清楚为什么它在控制台中不断循环吗?程序员应该列出用户插入的字符串的每个字符,并在每个唯一字符旁边的括号中显示该字符在字符串中出现的次数...不知道为什么。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main () {
string input;
cout << "input string: " , cin >> input;
sort (input.begin() , input.end());
while (!input.empty()) {
int j{1}, i{0};
while (input.at(i) == input.at(i+1)) {
j++;
i++;
}
cout << input.at(i) << " (" << j << "), ";
input.substr(i);
}
return 0;
}
本声明
input.substr(i);
不改变对象
input
本身。
因此,如果对于某些索引
i
input.at(i)
不等于 input.at(i+1)
,您将陷入无限循环,或者您可能会遇到超出范围的异常,因为 i + 1
可以等于 input.size()
。
来自成员函数的描述
at
抛出:out_of_range 如果 pos >= size()。
该计划可以通过不同的方式实施。例如下面的方式
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::cout << "input string: ";
std::string input;
std::cin >> input;
std::sort( input.begin() , input.end() );
for ( size_t i = 0; i < input.size(); )
{
size_t j = input.find_first_not_of( input[i], i );
if ( j == std::string::npos ) j = i + 1;
if ( i != 0 ) std::cout << ", ";
std::cout << input[i] << " (" << j - i << ")";
i = j;
}
std::cout << '\n';
return 0;
}
程序输出为
input string: Hello
H (1), e (1), l (2), o (1)
或者您可以使用标准容器
std::map
或 std::unordered_map
例如
#include <iostream>
#include <string>
#include <map>
int main()
{
std::cout << "input string: ";
std::string input;
std::cin >> input;
std::map<char, size_t> m;
for ( const auto &c : input )
{
++m[c];
}
bool first = true;
for ( const auto &p : m )
{
if ( !first ) std::cout << ", ";
std::cout << p.first << " (" << p.second << ")";
first = false;
}
std::cout << '\n';
return 0;
}
如果您希望输入字符串的字符按照它们在字符串中出现的顺序输出,那么程序可以如下所示
#include <iostream>
#include <string>
#include <map>
int main()
{
std::cout << "input string: ";
std::string input;
std::cin >> input;
auto less = [&input]( const auto &c1, const auto &c2 )
{
return input.find( c1 ) < input.find( c2 );
};
std::map<char, size_t, decltype( less )> m( less );
for ( const auto &c : input )
{
++m[c];
}
bool first = true;
for ( const auto &p : m )
{
if ( !first ) std::cout << ", ";
std::cout << p.first << " (" << p.second << ")";
first = false;
}
std::cout << '\n';
return 0;
}
或者在不更改原始字符串且不使用额外容器的情况下,程序可以如下所示。
#include <iostream>
#include <string>
int main()
{
std::cout << "input string: ";
std::string input;
std::cin >> input;
for ( size_t i = 0; i < input.size(); i++ )
{
size_t j = 0;
while ( j != i && input[j] != input[i] ) j++;
if ( j == i )
{
size_t count = 1;
while ( ++j < input.size() )
{
if ( input[j] == input[i] ) ++count;
}
if ( i != 0 ) std::cout << ", ";
std::cout << input[i] << " (" << count << ")";
}
}
std::cout << '\n';
return 0;
}
程序输出可能如下所示
input string: elephant
e (2), l (1), p (1), h (1), a (1), n (1), t (1)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
// Get input string from the user
string input;
cout << "input string: ";
cin >> input;
// Sort the characters in the input string
sort(input.begin(), input.end());
// Count and display occurrences of each character
while (!input.empty()) {
int count{1}, i{0};
// Count consecutive occurrences of a character
while (i < input.size() - 1 && input.at(i) == input.at(i + 1)) {
count++;
i++;
}
// Display the character and its count
cout << input.at(i) << " (" << count << "), ";
// Remove counted characters from the string
input = input.substr(i + 1);
}
return 0;
}