我正在迭代列表并通过向量.两者都充满了wstring s.
我需要比较他们指向的两个字符串,并找出"wstring1"是否存在于另一个"wstring2"中.
n和k是两个迭代器.
当我尝试:
wcscmp(*n,*k);
Run Code Online (Sandbox Code Playgroud)
如果我理解了错误消息,那么它会失败,因为const wchar t是正常的...
如何判断*n ="Hello you little fellow"是否包含*k ="little"?
案件没关系.
PS:我不想使用BOOST lib.
我创建了自定义函数将wstring转换为小写.但是,它在DebugMode中相当慢.是的,我知道ReleaseMode是重要的,但无论如何它是非常令人不安的.
wstring wstringToLower(wstring u)
{
wstring s;
for (int i=0;i<u.size();i++)
{
wstring sChar;
sChar=u.substr(i,1);
int iChar=static_cast<int>(sChar[0]);
int iNewChar=charCodeToLower(iChar);
wstring sNewChar=wstring(1,iNewChar);
s.append(sNewChar);
}
return s;
}
Run Code Online (Sandbox Code Playgroud)
有没有人看到任何明显可以改进以加速代码的东西,即使在DebugMode中也是如此?
谢谢!
在诸如如何将std :: string转换为小写的 SO帖子上?,我已经看过语法::something,例如
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
Run Code Online (Sandbox Code Playgroud)
而我想知道这意味着什么.我知道std :: transform查找transform名称空间内命名的函数或变量std.但是,当没有第一个参数时,范围运算符意味着什么?
我正在寻找lcase/ucase C++ STL类的最佳方法,我遇到了这篇文章:
给出的解决方案之一是:
#include <algorithm>
#include <string>
std::string data = “Abc”;
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
Run Code Online (Sandbox Code Playgroud)
但是,转换在stl_algo.h中定义为:
template<typename _InputIterator, typename _OutputIterator,
typename _UnaryOperation>
_OutputIterator
transform(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _UnaryOperation __unary_op)
{
...
Run Code Online (Sandbox Code Playgroud)
那么如何在不提供模板实例化参数的情况下调用呢?
为了澄清我的问题,我期待调用转换函数,如:
transform(std::string::iterator, std::string::iterator,
/* not sure what to put here for the predicate */);
Run Code Online (Sandbox Code Playgroud)
这是一次性(特殊情况),还是我遗漏了一些基本的东西?
我只是在学习C++(1周的经验),并且正在尝试编写一个输入验证循环,要求用户输入"是"或"否".我想通了,但感觉有更好的方法来接近这个.这是我想出的:
{
char temp[5]; // to store the input in a string
int test; // to be tested in the while loop
cout << "Yes or No\n";
cin.getline(temp, 5);
if (!(strcmp(temp, "Yes")) || !(strcmp(temp, "No"))) // checks the string if says Yes or No
cout << "Acceptable input"; // displays if string is indeed Yes or No
else //if not, intiate input validation loop
{
test = 0;
while (test == 0) // loop
{
cout << "Invalid, try again.\n"; …Run Code Online (Sandbox Code Playgroud) 我有一个矢量word_list,我想在word_list中找到一个单词.我用的是功能
bool Mylayer::existWord(vector<string> word_list, string word)
{
if (std::lower_bound(word_list.begin(), word_list.end(), word) != word_list.end())
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但它并不确切.有人可以告诉我为什么吗?
我有一个要求,其中我的 C++ 代码需要进行不区分大小写的比较,而不必担心字符串是否已编码,或者所涉及的编码类型。该字符串可以是 ASCII 或非 ASCII,我只需要按原样存储它并将其与第二个字符串进行比较,而无需考虑是否设置了正确的语言环境等等。
用例:假设我的应用程序接收一个字符串(假设它是一个文件名)最初为“Zoë Saldaña.txt”,并按原样存储它。随后,它接收另一个字符串“zoë saLdañA.txt”,并且通过使用一些 API,该字符串与第一个字符串之间的比较应该会导致匹配。与文件名“abc.txt”和“AbC.txt”相同。
我阅读了 IBM 的 ICU 以及它默认如何使用 UTF-16 编码。我很想知道:
如果 ICU 提供了一种通过无缝处理字符串来解决我的需求的方法,而不管它们的编码类型如何?
如果 1. 的答案是否定的,那么,使用 ICU 的 API,将所有字符串(ASCII 和非 ASCII)规范化为 UTF-16,然后进行不区分大小写的比较和其他操作是否安全?
是否有替代方案可以促进这一点?
我读了这篇文章,但它不太符合我的要求。
谢谢!
我想知道如何__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >在 C++ 中将字符迭代器转换为字符串?
#include <iostream>\n#include <string>\n#include <algorithm>\nusing std::string;\n\n#define TO_UPPER(s) (transform((s).begin(), (s).end(), (s).begin(), ::toupper))\n#define TO_LOWER(s) (transform((s).begin(), (s).end(), (s).begin(), ::tolower))\n\n\nint main() {\n string str = "Hello world!";\n\n string result = TO_LOWER(str);\n\n std::cout << result << std::endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n错误:
\n[1/2] Building CXX object CMakeFiles/untitled.dir/main.cpp.o\nFAILED: CMakeFiles/untitled.dir/main.cpp.o \n/usr/bin/c++ -g -std=gnu++11 -MD -MT CMakeFiles/untitled.dir/main.cpp.o -MF CMakeFiles/untitled.dir/main.cpp.o.d -o CMakeFiles/untitled.dir/main.cpp.o -c /home/amir-pc/CLionProjects/untitled/main.cpp\n/home/amir-pc/CLionProjects/untitled/main.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n/home/amir-pc/CLionProjects/untitled/main.cpp:7:31: error: conversion from \xe2\x80\x98__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >\xe2\x80\x99 to non-scalar type \xe2\x80\x98std::string\xe2\x80\x99 {aka \xe2\x80\x98std::__cxx11::basic_string<char>\xe2\x80\x99} requested\n 7 …Run Code Online (Sandbox Code Playgroud) 所以我遇到了麻烦.我的教授希望我写一个基本程序,如果它是一个住宅套餐或使用if else语句的商业套餐,它会增加成本并为有线电视公司输出费率.我唯一的问题是他希望用户能够输入'R'或'r'大写或更低的基数,与'B'或'b'相同.
我这样做
if(customer_Type=='R' || 'r')
Run Code Online (Sandbox Code Playgroud)
但如果我使用除R或r之外的任何东西,它就不会移动到下一个if语句.程序下面的代码完全符合我的要求但只是没有小写字母
cout<<"Welcome to Cable Company billing procedure.\n";
cout<<"Enter your account number : ";
cin>>customer_Account_Number;
cout<<"Enter your customer type (residential input R or business input B) : ";
cin>>customer_Type;
Run Code Online (Sandbox Code Playgroud)
-
if(customer_Type=='R') // If residential
{
cout<<"How many premium channels have you subscribed to?\n";
cin>>num_Of_Prem_Channels;
amount_Due = ResBillProcFee + ResBasicServCost + ResCostPremChannels * num_Of_Prem_Channels;
cout<<"Your residential bill is $"<<amount_Due<<endl;
}
else if(customer_Type=='B')
{
cout<<"Enter number of premium channels\n";
cin>>num_Of_Prem_Channels;
cout<<"Enter number of basic service connections\n";
cin>>num_Of_Basic_Service_Connections; …Run Code Online (Sandbox Code Playgroud) c++ ×9
string ×3
c++11 ×1
cocos2d-x ×1
compare ×1
icu ×1
input ×1
loops ×1
optimization ×1
performance ×1
templates ×1
unicode ×1
validation ×1