使用replace(CharSequence target, CharSequence replacement)String中的方法,如何使目标不区分大小写?
例如,它现在的工作方式:
String target = "FooBar";
target.replace("Foo", "") // would return "Bar"
String target = "fooBar";
target.replace("Foo", "") // would return "fooBar"
Run Code Online (Sandbox Code Playgroud)
我怎样才能使它如此替换(或者如果有更合适的方法)是不区分大小写的,这样两个例子都返回"Bar"?
围绕未定义行为(UB)的大多数对话都讨论了如何有一些平台可以做到这一点,或者一些编译器会这样做.
What if you are only interested in one platform and only one compiler (same version) and you know you will be using them for years?
Nothing is changing but the code, and the UB is not implementation-defined.
Once the UB has manifested for that architecture and that compiler and you have tested, can't you assume that from then on whatever the compiler did with the UB the first time, it will do that every time?
Note: I …
是什么const在"顶级"预选赛中,C平均++?
还有什么其他水平?
例如:
int const *i;
int *const i;
int const *const i;
Run Code Online (Sandbox Code Playgroud) 我编译以下代码,但我在Visual Studio中得到一个我无法理解的编译错误.
#include <iostream>
using namespace std;
int main()
{
int matchCount, findResult;
long childPID;
string userInput = "blank";
// string to be searched through
string longString = "The PPSh-41 is a Soviet submachine gun designed by Georgi Shpagin as an inexpensive, simplified alternative to the PPD-40.";
while (userInput.compare("!wq"));
{
// reset variables for reuse
matchCount = 0;
findResult = -1;
cout << "Please enter a word/s to search for (!wq to exit): "; // prompts user for string to …Run Code Online (Sandbox Code Playgroud) 给定std::bitset<64> bits任意数量的位和位位置X(0-63)
在X位或更低位计数位的最有效方法是什么,如果未设置X位,则返回0
注意:如果设置该位,则返回始终至少为1
蛮力方式很慢:
int countupto(std::bitset<64> bits, int X)
{
if (!bits[X]) return 0;
int total=1;
for (int i=0; i < X; ++i)
{
total+=bits[i];
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
这个count()方法bitset将为您popcount提供所有位,但bitset不支持范围
注意:这不是如何计算32位整数中的设置位数?因为它询问所有位而不是0到X的范围
我有一个用例,我需要将签名值转换为unsigned,以使值可排序.我需要这个char,short,int,long,和long long
通过排序,我的意思是对于signed类型X,如果(a < b)然后转换为无符号的converted(a) < converted(b).请注意,在许多情况下,从负值signed直接转换为unsigned值将使值大于0并打破此约束(二进制补码实现)
最简单的想法char是:
unsigned char convert(char x)
{
return (unsigned char)(x ^ 0x80); // flip sign to make it sortable
}
Run Code Online (Sandbox Code Playgroud)
但这似乎是undefined behavior.
虽然有可能转换为更大的类型,添加类型MIN值,并转换为unsigned类型,我不确定这是否更合规,并且不能用于long long
如何在没有任何undefined behavior类型的情况下完成这项工作?
使用转换似乎是安全的memcpy,但不清楚如何以兼容的方式维护排序顺序.
(注意,这类似于:没有兼容的方式来转换相同大小的有符号/无符号,除了我需要保持排序顺序的结果)
如果你有一个C文件,用C编译器编译并且已经为C而不是C++定义了行为,你可以将它与C++文件链接而不是没有未定义的行为吗?
在blah.c中(编译为C的文件)
struct x {
int blah;
char buf[];
};
extern char * get_buf(struct x * base);
extern struct x * make_struct(int blah, int size);
Run Code Online (Sandbox Code Playgroud)
blah_if.h
extern "C"
{
struct x;
char * get_buf(struct x * base);
struct x * make_struct(int blah, int size);
}
Run Code Online (Sandbox Code Playgroud)
some_random.cpp(用C++编译器编译)
#include "blah_if.h"
...
x * data=make_struct(7, 12);
std::strcpy(get_buf(data), "hello");
Run Code Online (Sandbox Code Playgroud)
在使用C编译器编译的文件中使用C的灵活数组成员中定义的行为,当被编译为C++并与C编译器中的对象链接时使用的定义行为?
请注意,因为使用了C编译器并且它struct x是不透明的,所以这与以下内容不同:
如果有一个类需要一个指针和一个bool.为简单起见,int将在示例中使用指针,但指针类型无关紧要,只要它指向size()大于1的内容即可.
使用{ bool , int *}数据成员定义类将导致类的大小是指针大小的两倍,并浪费大量空间
如果指针未指向char(或其他数据size(1)),那么可能低位将始终为零.该课程可以定义为{int *}或方便:union { int *, uintptr_t }
将bool通过设置/清除指针的低位按照逻辑实现的bool价值和清除该位,当你需要使用指针.
定义的方式:
struct myData
{
int * ptr;
bool flag;
};
myData x;
// initialize
x.ptr = new int;
x.flag = false;
// set flag true
x.flag = true;
// set flag false
x.flag = false;
// use ptr
*(x.ptr)=7;
// change ptr
x = y; // y …Run Code Online (Sandbox Code Playgroud) 如果将extern C它与C++文件一起使用,是否允许在C++中未定义的已定义C行为?
blah.h
extern "C"
{
struct x {
int blah;
char buf[];
};
char * get_buf(struct x * base);
struct x * make_struct(int blah, int size);
}
Run Code Online (Sandbox Code Playgroud)
some_random.cpp
#include "blah.h"
...
x * data=make_struct(7, 12);
std::strcpy(get_buf(data), "hello");
Run Code Online (Sandbox Code Playgroud)
在C的灵活数组成员中使用定义的行为,在使用这种方式时定义了行为吗?
如果您有以下内容:
if (x)
{
y = *x;
}
else
{
y = 0;
}
Run Code Online (Sandbox Code Playgroud)
然后保证定义行为,因为我们只能取消引用x它是否为0
可以这样说:
y = (x) ? *x : 0;
Run Code Online (Sandbox Code Playgroud)
这似乎按预期工作(甚至用-Wpedanticg ++编译)
这有保证吗?
c++ conditional-operator short-circuiting undefined-behavior
c++ ×9
c ×2
algorithm ×1
casting ×1
const ×1
header ×1
java ×1
optimization ×1
performance ×1
pointers ×1
portability ×1
replace ×1
string ×1
substring ×1