我有两个char*
我要比较的邮政编码,忽略了大小写.有这个功能吗?
或者我是否必须遍历每个使用tolower函数然后进行比较?
知道这个函数如何对字符串中的数字做出反应
谢谢
我正在写这个答案,我引自http://en.cppreference.com/w/cpp/string/byte/tolower#Parameters
不能表示为unsigned char且不等于EOF,行为未定义
当我去检查添加了这个短语的编辑时,我发现了作者的评论:
根据C99 7.4/1,不能对任何ctype.h函数使用负符号字符
作者引用C++文档中的C99标准.这有效吗?我在C++标准中找不到关于此函数定义的任何内容,因此我必须假设它是有效的.
但这有两个原因让我担忧:
鉴于string foo
,我已经写了关于如何使用's 将字符转换为小写的答案cctype
tolower
transform(cbegin(foo), cend(foo), begin(foo), static_cast<int (*)(int)>(tolower))
Run Code Online (Sandbox Code Playgroud)
但我已经开始考虑 locale
的tolower
,这可以这样使用:
use_facet<ctype<char>>(cout.getloc()).tolower(data(foo), next(data(foo), foo.size()));
Run Code Online (Sandbox Code Playgroud)
tolower
接受并返回一个int
我认为只是一些过时的C东西的事实?我写了下面的代码来检查输入(answer3)是一个数字还是字符串,如果它不是一个数字,它应该返回"仅输入数字",但它返回相同的数字.请建议我一个解决方案.
#include <iostream>
#include <string>
#include <typeinfo>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
int main ()
{
string ques1= "Client's Name :";
string ques2 = "Client's Address :";
string ques3 = "Mobile Number :";
char answer1 [80];
string answer2;
int answer3;
cout<<ques1<<endl;
cin>>answer1;
cout<<ques2<<endl;
cin>>answer2;
cout<<ques3<<endl;
cin>>answer3;
if (isdigit(answer3))
{
cout<<"Correct"<<endl;
}
else
{
cout<<"Enter Numbers Only"<<endl;
}
system("pause>null");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以检查字符串中的一个字母是否大写.其他查看方式,如果字符串中的所有字母都是大写或小写.例:
string a = "aaaaAaa";
string b = "AAAAAa";
if(??){ //Cheking if all the string is lowercase
cout << "The string a contain a uppercase letter" << endl;
}
if(??){ //Checking if all the string is uppercase
cout << "The string b contain a lowercase letter" << endl;
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
为什么不能更简单地调用STL函数?我在cppreference.com上查看以下代码片段:
#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
int main()
{
std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return std::toupper(c); });
std::cout << s;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,应该可以使这个电话更简短.第一个显而易见的事情是取消lambda:
std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
std::cout << s;
Run Code Online (Sandbox Code Playgroud)
但这不起作用.由于您通常想要转换整个容器,因此应该可以将其用作参数:
std::string s("hello");
std::transform(s, s.begin(), std::toupper);
std::cout << s;
Run Code Online (Sandbox Code Playgroud)
您还可以省略输出迭代器以按值返回结果:
std::string s("hello");
std::cout << std::transform(s, std::toupper);
Run Code Online (Sandbox Code Playgroud)
此时临时变量是不必要的:
std::cout << std::transform("hello"s, std::toupper);
Run Code Online (Sandbox Code Playgroud)
增加了可读性:
using namespace std;
cout << transform("hello"s, toupper);
Run Code Online (Sandbox Code Playgroud)
这不是更可读,更好吗?为什么STL函数不能设计为允许编写这样的简短代码?是否可以在未来版本的C++标准中缩短这些调用?
我正在学习c++字符串中使用的std::char_traits,然后我意识到std::char_traits可以用来创建自定义字符串类,就像cplusplus.com的文章中的case_insensitive_string一样,我将粘贴下面的代码示例。
// char_traits::compare
#include <iostream> // std::cout
#include <string> // std::basic_string, std::char_traits
#include <cctype> // std::tolower
#include <cstddef> // std::size_t
// case-insensitive traits:
struct custom_traits: std::char_traits<char> {
static bool eq (char c, char d) { return std::tolower(c)==std::tolower(d); }
static bool lt (char c, char d) { return std::tolower(c)<std::tolower(d); }
static int compare (const char* p, const char* q, std::size_t n) {
while (n--) {if (!eq(*p,*q)) return lt(*p,*q); ++p; ++q;}
return 0;
}
};
int main ()
{
std::basic_string<char,custom_traits> …
Run Code Online (Sandbox Code Playgroud) 检测文件路径中是否有任何非 ASCII 字符
我有一个 UTF-8 编码的 Unicode 字符串,用于存储文件路径,例如C:\Users\myUser\Downloads\ü.pdf。我已经检查过该字符串在本地文件系统中是否包含正确的文件路径,但是由于我将此字符串发送到仅支持 ASCII 的其他进程,因此我需要确定该字符串是否包含任何非 ASCII 字符。
我怎样才能做到这一点?
给定一个std::vector
字符串,删除从末尾开始的所有空元素(等于空字符串或空格)的最佳方法是什么。当发现非空元素时,应停止删除元素。
我当前的方法(正在进行中)类似于:
while (Vec.size() > 0 && (Vec.back().size() == 0 || is_whitespace(Vec.back()))
{
Vec.pop_back();
}
Run Code Online (Sandbox Code Playgroud)
whereis_whitespace
返回一个 bool 值,说明字符串是否为空格
我怀疑我的方法会在每次迭代时调整向量的大小,这是次优的。也许通过某种算法可以一步完成。
输入:{ "A", "B", " ", "D", "E", " ", "", " " }
所需输出:{“A”,“B”,“”,“D”,“E”}