[英]Infinite loop using string.substr(post,len)
Could you figure out why it keeps looping infinitely in the console?你能弄清楚为什么它会在控制台中无限循环吗? The programmer's supposed to list out each character of a user-inserted string and next to each unique character, in brackets, it's supposed to display the number of times that character occurs in the string... no idea why.
程序员应该列出用户插入的字符串的每个字符,并在每个唯一字符旁边的括号中显示该字符在字符串中出现的次数......不知道为什么。
#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;
}
This statement这个说法
input.substr(i);
does not change the object input
itself.不会改变 object
input
本身。
So, either you will have an infinite loop if for some index i
input.at(i)
is not equal to input.at(i+1)
, or you can have an exception out of range because i + 1
can be equal to input.size()
.因此,如果对于某些索引
i
input.at(i)
不等于input.at(i+1)
,您将有一个无限循环,或者您可以有一个超出范围的异常,因为i + 1
可以等于输入. input.size()
。
From the description of the member function at
从成员 function 的描述
at
Throws: out_of_range if pos >= size().
抛出:如果 pos >= size(),则超出范围。
The program can be implemented in different ways.该程序可以以不同的方式实施。 For example the following way
例如以下方式
#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;
}
The program output is程序 output 是
input string: Hello
H (1), e (1), l (2), o (1)
Or you can use the standard container std::map
or std::unordered_map
as for example或者您可以使用标准容器
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;
}
If you want that characters of the inputted string were output in the order in which they are present in the string then the program can look like如果您希望输入字符串的字符按照它们在字符串中出现的顺序是 output,那么程序可以看起来像
#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;
}
Or without changing the original string and without using an additional container the program can look the following way.或者在不更改原始字符串且不使用其他容器的情况下,程序可以如下所示。
#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;
}
The program output might look like程序 output 可能看起来像
input string: elephant
e (2), l (1), p (1), h (1), a (1), n (1), t (1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.