我在另一个代码中看到了第二个,我想这个长度比较是为了提高代码生产率.它在解析器中用于具有特定字典的脚本语言:单词长度为4到24个字母,平均为7-8个字母,字母表包括26个拉丁字母加上"@","$"和"_".
长度比较用于转义==运算符使用STL字符串,这显然需要比简单整数比较更多的时间.但同时在给定字典中的第一个字母分布仅比字大小的分布宽,因此比较字符串的两个第一个字母通常通常不同于该字符串的大小.这使长度比较变得不必要.
我已经运行了一些测试,这就是我发现的:测试两个随机字符串比较百万次,第二种方式更快,所以长度比较似乎是有帮助的.但是在一个工作项目中,它在调试模式下运行速度更慢,在发布模式下运行速度更快.
所以,我的问题是:为什么长度比较可以加强比较,为什么它可以减缓它?
UPD:我也不喜欢那种第二种方式,但我认为这是有原因的,我想知道,这是什么原因.
UPD2:说真的,问题不在于如何做到最好.在这种情况下,我甚至不再使用STL字符串了.毫无疑问,长度比较是不必要的和错误的等等.奇迹是 - 它确实在某个特定测试中稍微好一些.这怎么可能?
我发现这个非常好的代码片段将字符串转换为System:String^如下:
System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string
Run Code Online (Sandbox Code Playgroud)
我正在将rtn传递给C#程序.无论如何,在这个代码存在的函数内部,我传入了一个System::String^.我还发现了一些System:String^使用以下代码将a转换为字符串的代码:
pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String
size_t convertedChars = 0;
size_t sizeInBytes = ((cmd->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,ch, sizeInBytes,wch, sizeInBytes);
Run Code Online (Sandbox Code Playgroud)
现在我可以使用'ch'作为字符串.
然而,这似乎比使用其他方式转换更多的工作gcnew.所以,最后我的问题是,是否有一些东西将System::String^使用与gcnew方式类似的方式将a转换为字符串?
可能重复:
使用逗号分隔值比较两个字符串
我在C++工作,我有两个字符串:
string str1 = "1,4,8,",
str2 = "4,1,8,";
Run Code Online (Sandbox Code Playgroud)
两个字符串都包含逗号分隔值.现在我只想检查是否所有元素都str1存在str2,无论它们的位置如何.有没有直接的方法来检查这个?我需要为此编写自定义代码吗?
我需要.在C++中拆分字符串..
下面是我的字符串 -
@event.hello.dc1
现在我需要拆就.在上面的字符串,并从中获取@event,然后传递@event到下面的方法-
bool upsert(const char* key);
以下是我从这里阅读后到目前为止的代码-
void splitString() {
string sentence = "@event.hello.dc1";
istringstream iss(sentence);
copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
但我无法理解如何@event通过拆分.使用上述方法来提取,因为上述方法仅适用于空白...以及如何通过拆分提取该字符串中的所有内容,.如下所述 -
split1 = @event
split2 = hello
split3 = dc1
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助..
假设我有一个从自定义内存池分配的char*并填充了必要的数据.我在std :: string类的assign方法中使用了char*,并确保使用std :: move调用move varient.
void DoWork(char* my_pool_allocated_buffer) {
std::string s;
s.assign(std::move(my_pool_allocated_buffer));
//do some work with s
}
Run Code Online (Sandbox Code Playgroud)
我有两个问题.
在Alexandrescu 基于策略的设计中我不理解的是创建没有任何共同点的新类型,在我看来,仍然存在许多应该以某种方式表示的共同点.
例如,std::string和std::basic_string<>:allocator是非常内部的东西,在我看来,使用该类的代码不应该知道该类正在使用哪个分配器.
但是,因为已经创建了一个新类型,所以std::basic_string_1,所有那些传递的std::string&方法基本上都被破坏了,我看不出一个有效的理由,为什么std::basic_string<>不同的分配器被认为std::basic_string<>与另一个分配器完全不同.
我的问题是:为什么每个人都没有共同的父母std::basic_string<>,这样可以避免这个问题?一般在我的代码中,当我有一个Whatever<T>,我使它继承WhateverBase某种类型,当T它没有显示在该类的公共接口上时,它工作得非常好......
我想创建一个动态矢量,每个矢量元素都是一个字符串数组.
我想到的数据结构是这样的:
VECTOR:
[0] = [str1, str2, str3]
[1] = [str1, str2, str3]
[2] = [str1, str2, str3]
Run Code Online (Sandbox Code Playgroud)
我正确地将值插入res变量但是我的代码无法正常工作:打印循环执行4次,但每次只打印LAST元素.我相信问题可能是:1)我没有在向量中正确推送字符串数组; 2)当我想打印它时,我没有正确管理向量和所有字符串的迭代.
这是我的代码:
std::vector<std::string*> DatabaseConnector::ExecuteQuery(std::string query, std::vector <std::string> columns)
{
std::vector<std::string*> results;
std::string res[columns.size() + 1]; // last value = '\0' to signal end of array
db_params.res = db_params.stmt->executeQuery(query);
while (db_params.res->next()) // Access column data by alias or column name
{
int i = 0;
for(std::string s : columns)
res[i++] = db_params.res->getString(s);
res[i] = "\0";
results.push_back(res);
}
for(auto el :results)
{
int …Run Code Online (Sandbox Code Playgroud) 我想知道读取std::string对象中最后一行的最快方法是什么意思是最后一次以最快的方式
出现后的字符串\n?
我创建了简单的C ++ std::string值。
但是该值会产生意想不到的结果。
我使用g ++编译器(Linux)和Visual Studio(Windows)测试了此代码,并且两个编译器都出现相同的问题。
正常结果码
/* This code results are Normal */
#include <bits/stdc++.h>
int main() {
std::string a1 = "a1";
std::string a2 = "a2";
std::string b1("b1");
std::string b2("b2");
const char *c1 = std::string("c1").c_str();
const char *c2 = std::string("c2").c_str();
std::cout << "Expected [a1], real [" << a1 << "]\n";
std::cout << "Expected [a2], real [" << a2 << "]\n";
std::cout << "Expected [b1], real [" << b1 << "]\n";
std::cout << "Expected [b2], real [" << …Run Code Online (Sandbox Code Playgroud) 我有一个类,其中包含一个字符串作为私有成员。该类具有公共成员函数,它们都将const ref返回给std :: wstring。
返回的所有const-ref字符串都是私有成员的子字符串。
我可以将每个子字符串存储为成员,然后将const ref返回给那些成员,但这并不是最佳选择,因为它会复制内存。
假设我确实存储了足够的内存来知道每个子字符串的开始和结束索引。
如何在不复制任何成员字符串的情况下实现上述类?