我正在尝试检查字符串是否包含C中的子字符串,如:
char *sent = "this is my sample example";
char *word = "sample";
if (/* sentence contains word */) {
/* .. */
}
Run Code Online (Sandbox Code Playgroud)
什么是使用而不是string::find
在C++中?
我已经试了:
class Foo(bar: Int)
Run Code Online (Sandbox Code Playgroud)
VS:
class Foo(private val bar: Int)
Run Code Online (Sandbox Code Playgroud)
而且他们似乎表现得一样,虽然我找不到任何地方说(bar:
Int)
扩大到(private val bar: Int)
所以我的问题是,这些相同/相似吗?
在旁注中,我一直在尝试使用-Xprint:typer
这些代码片段,除了第二行中的额外行之外,它们产生相同的代码.我该如何阅读额外的行?
..
class Foo extends scala.AnyRef {
<paramaccessor> private[this] val bar: Int = _;
def <init>(bar: Int): this.Foo = {
Foo.super.<init>();
()
}
}
..
..
class Foo extends scala.AnyRef {
<paramaccessor> private[this] val bar: Int = _;
<stable> <accessor> <paramaccessor> private def bar: Int = Foo.this.bar;
def <init>(bar: Int): this.Foo = {
Foo.super.<init>();
()
}
}
..
Run Code Online (Sandbox Code Playgroud) 我有这个方法:
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,编译器向我显示了一个问题:
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
Run Code Online (Sandbox Code Playgroud)
因为managedQuery()
已弃用.
如何在不使用的情况下重写此方法managedQuery()
?
当我malloc
在C程序中使用时,我收到警告:
warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
Run Code Online (Sandbox Code Playgroud)
然后我可以包括<malloc.h>
或<stdlib.h>
摆脱warning
它虽然它没有它也可以工作.
所以我想知道,gcc
当我不包含任何内容时,这些标题之间有什么区别?
(我使用ubuntu 12.04 64-bit
同gcc 4.6.3
)
我有兴趣找到第n行pascal三角形(不是特定元素,而是整行本身).最有效的方法是什么?
我想到了通过总结上面行中相应元素来构造三角形的传统方法:
1 + 2 + .. + n = O(n^2)
Run Code Online (Sandbox Code Playgroud)
另一种方法可能是使用特定元素的组合公式:
c(n, k) = n! / (k!(n-k)!)
Run Code Online (Sandbox Code Playgroud)
对于行中的每个元素,我认为根据计算组合的方式,前一种方法需要更多时间.有任何想法吗?
algorithm combinations binomial-coefficients pascals-triangle
编辑在你兴奋之前,最后看到重要的编辑,如果你仍然好奇,这些被报道为:
我一直在尝试一段代码,并惊讶地发现我没有得到堆栈溢出.试图简化我甚至得到的东西:
#include <stdio.h>
int main()
{
int i;
/* 1,500,000,000 x 4 bytes = 6,000,000,000 bytes = 6GB */
int size = 1500000000;
int arr[size];
for (i = 0; i < size; i++) {
arr[i] = 1;
}
printf("first: %d\n", arr[0]);
printf("last: %d\n", arr[size - 1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这使我怀疑我甚至不知道内存管理的基础知识.我认为arr[size]
应该轻松分配堆栈和溢出,但它使用我的所有内存和交换并成功完成.我错过了什么?
我试过gcc
和clang
版本:
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
Run Code Online (Sandbox Code Playgroud)我关闭了优化(-O0
) …
如果我有一个基类,只有基类的虚方法和2个派生类,那么实现了这些虚方法.
我如何能:
// causes C2259
BaseClass* base = new BaseClass[2];
BaseClass[0] = new FirstDerivedClass;
BaseClass[1] = new SecondDerivedClass;
Run Code Online (Sandbox Code Playgroud)
要么:
// causes "base is being used without being initialized"
BaseClass* base;
// causes CC59 again
BaseClass* base = new BaseClass;
base[0] = FirstDerivedClass();
base[1] = SecondDerivedClass();
Run Code Online (Sandbox Code Playgroud)
(或类似的东西)
...所以我可以BaseClass
通过它访问s方法DerivedClass
,但是通过指针和指针是一个DerivedClass
s 的数组?
请参阅以下代码段.我想使用std::bind
for重载函数foobar
.它只调用没有参数的方法.
#include <functional>
#include <iostream>
class Client
{
public :
void foobar(){std::cout << "no argument" << std::endl;}
void foobar(int){std::cout << "int argument" << std::endl;}
void foobar(double){std::cout << "double argument" << std::endl;}
};
int main()
{
Client cl;
//! This works
auto a1 = std::bind(static_cast<void(Client::*)(void)>(&Client::foobar),cl);
a1();
//! This does not
auto a2= [&](int)
{
std::bind(static_cast<void(Client::*)(int)>(&Client::foobar),cl);
};
a2(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当我尝试使用vim中的块选择从行尾删除时,我得到了一个不寻常的行为.
所以,假设我有一个文本:
delete this char:x
and this:x
also this:x
and then this:x
lastly this:x
Run Code Online (Sandbox Code Playgroud)
如果我想附加y
到每行我可以:
4j
$
A
y
为了得到:
delete this char:xy
and this:xy
also this:xy
and then this:xy
lastly this:xy
Run Code Online (Sandbox Code Playgroud)
但如果我尝试x
在最后一步删除而不是追加,我希望得到:
delete this char:
and this:
also this:
and then this:
lastly this:
Run Code Online (Sandbox Code Playgroud)
虽然我最终得到:
delete this char:
and this:x:
also this:x:
and then this:x:
lastly this:x:
Run Code Online (Sandbox Code Playgroud)
据我了解它附加在第一线,所有其它线路的最后一个字符(在这种情况下:
),而不是删除缺少的(在这种情况下x
).
我可以用宏或替代品做到这一点,但我不太明白这种行为背后的基本原理.有没有办法用块选择来做到这一点?