C++ 计算字符串中尾随空格的数字

C++ 计算字符串中尾随空格的数字,c++,string,C++,String,有人能帮我降低下面代码的复杂性吗?它计算字符串中尾随空格的数量 void main() { string url = "abcd "; int count = 1; for (int i = 0; i < url.length(); i++) { if (url.at(i) == ' ') { for (int k = i + 1; k < url.length(); k++)

有人能帮我降低下面代码的复杂性吗?它计算字符串中尾随空格的数量

void main()
{
    string url = "abcd   ";
    int count = 1;
    for (int i = 0; i < url.length(); i++)
    {
        if (url.at(i) == ' ')
        {
            for (int k = i + 1; k < url.length(); k++)
            {
                if (url.at(k) != ' ')
                {
                    count = 1;
                    break;
                }
                else
                {
                    count++;
                    i++;
                }
            }
        }
    }
cout<< count;
}
void main()
{
字符串url=“abcd”;
整数计数=1;
for(int i=0;i
函数的
find_last_not_返回
string::npos
,它只是
(size\t)-1
,是
size\u t
的最大值。从长度中减去该值,然后再减去1,所得值就是长度。该算法在所有情况下都有效

请注意,如果字符串中没有非空格字符(字符串为空或仅包含空格),
函数的
find_last_not_返回
string::npos
,它只是
(size\t)-1
,是
size\u t
的最大值。从长度中减去该值,然后再减去1,所得值就是长度。该算法在所有情况下都有效

请注意,如果字符串中没有非空格字符(字符串为空或仅包含空格),
函数的
find_last_not_返回
string::npos
,它只是
(size\t)-1
,是
size\u t
的最大值。从长度中减去该值,然后再减去1,所得值就是长度。该算法在所有情况下都有效


请注意,如果字符串中没有非空格字符(字符串为空或仅包含空格),
函数的
find_last_not_返回
string::npos
,它只是
(size\t)-1
,大小的最大值。\u t
。从长度中减去该值,然后再减去1,所得值就是长度。该算法在所有情况下都有效。

开始反向计算空白

void main() {
    string url = "abcd   ";
    int count = 0;
    for (int i = url.length(); i > 0; i--) {
        if (url.at(i) == ' ') {
            count++;                
        } else {
            break;
        }
    }
cout<< count;
}
void main(){
字符串url=“abcd”;
整数计数=0;
对于(int i=url.length();i>0;i--){
如果(url.at(i)=''){
计数++;
}否则{
打破
}
}

不能开始反向计算空白

void main() {
    string url = "abcd   ";
    int count = 0;
    for (int i = url.length(); i > 0; i--) {
        if (url.at(i) == ' ') {
            count++;                
        } else {
            break;
        }
    }
cout<< count;
}
void main(){
字符串url=“abcd”;
整数计数=0;
对于(int i=url.length();i>0;i--){
如果(url.at(i)=''){
计数++;
}否则{
打破
}
}

不能开始反向计算空白

void main() {
    string url = "abcd   ";
    int count = 0;
    for (int i = url.length(); i > 0; i--) {
        if (url.at(i) == ' ') {
            count++;                
        } else {
            break;
        }
    }
cout<< count;
}
void main(){
字符串url=“abcd”;
整数计数=0;
对于(int i=url.length();i>0;i--){
如果(url.at(i)=''){
计数++;
}否则{
打破
}
}

不能开始反向计算空白

void main() {
    string url = "abcd   ";
    int count = 0;
    for (int i = url.length(); i > 0; i--) {
        if (url.at(i) == ' ') {
            count++;                
        } else {
            break;
        }
    }
cout<< count;
}
void main(){
字符串url=“abcd”;
整数计数=0;
对于(int i=url.length();i>0;i--){
如果(url.at(i)=''){
计数++;
}否则{
打破
}
}

cout您可以使用find_if()算法函数执行此操作:

#include <algorithm>
#include <string>
#include <cctype>
#include <iostream>

using namespace std;

int main()
{
    string s = "This string has spaces at the end    ";
    string::iterator it = 
         find_if(s.rbegin(), s.rend(), [](char ch) { return !isspace(ch); }).base();
    cout << "The number of trailing spaces is " << std::distance(it, s.end());
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
string s=“此字符串末尾有空格”;
字符串::迭代器它=
查找_if(s.rbegin(),s.rend(),[](char ch){return!isspace(ch);}).base();

cout您可以使用find_if()算法函数执行此操作:

#include <algorithm>
#include <string>
#include <cctype>
#include <iostream>

using namespace std;

int main()
{
    string s = "This string has spaces at the end    ";
    string::iterator it = 
         find_if(s.rbegin(), s.rend(), [](char ch) { return !isspace(ch); }).base();
    cout << "The number of trailing spaces is " << std::distance(it, s.end());
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
string s=“此字符串末尾有空格”;
字符串::迭代器它=
查找_if(s.rbegin(),s.rend(),[](char ch){return!isspace(ch);}).base();

cout您可以使用find_if()算法函数执行此操作:

#include <algorithm>
#include <string>
#include <cctype>
#include <iostream>

using namespace std;

int main()
{
    string s = "This string has spaces at the end    ";
    string::iterator it = 
         find_if(s.rbegin(), s.rend(), [](char ch) { return !isspace(ch); }).base();
    cout << "The number of trailing spaces is " << std::distance(it, s.end());
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
string s=“此字符串末尾有空格”;
字符串::迭代器它=
查找_if(s.rbegin(),s.rend(),[](char ch){return!isspace(ch);}).base();

cout您可以使用find_if()算法函数执行此操作:

#include <algorithm>
#include <string>
#include <cctype>
#include <iostream>

using namespace std;

int main()
{
    string s = "This string has spaces at the end    ";
    string::iterator it = 
         find_if(s.rbegin(), s.rend(), [](char ch) { return !isspace(ch); }).base();
    cout << "The number of trailing spaces is " << std::distance(it, s.end());
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
string s=“此字符串末尾有空格”;
字符串::迭代器它=
查找_if(s.rbegin(),s.rend(),[](char ch){return!isspace(ch);}).base();


如果你的代码工作正常,你想让你的代码被审核,也许最合适的地方是。谢谢。在CUT中,它是一个C++程序。为什么<代码> C <代码>标签???HimangSU不,最好使用<代码>。
。它使代码清晰易读。我相信所有现代编译器都可以优化这些东西。一个循环就足够了;通过迭代字符串最后一个字符(从右到左)的字符来计算它当你找到一个非空格字符时,你就要中断。为什么你要把C++中的字符常数<代码> >代码>和另一个地方的硬编码的ASCII值32进行比较?如果你的代码工作正常,你希望你的代码被审核,也许最合适的地方是。谢谢。m、 为什么使用
c
标记?@Himanshu不,最好使用
.length()
。它使代码清晰易读。我相信所有现代编译器都可以优化这些东西。一个循环就足够了;通过迭代字符串最后一个字符(从右到左)的字符来计算它并在找到非空格字符时中断