我正在使用实现 LocationListener 类的服务。现在我想知道设备的当前位置(纬度,经度)。我使用了 requestLocationUpdates 方法,以便在位置更改时调用 onLocationChanged 方法。我希望 onLocationChanged 方法只应调用一次,即当我第一次获取位置时,不应再次调用它。我已经调用 stopSelf 方法来停止 onLocationChanged 方法中的服务。但我仍然收到位置更新。请帮我。
今天,我对c结构与c ++结构的行为有点惊讶.
fun.cpp:http://ideone.com/5VLPC
struct nod
{
static int i;
};
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述程序运作完美.
但,
当在C环境中运行相同的程序时,它会给出错误:
prog.c:3: error: expected specifier-qualifier-list before ‘static’
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我想实现一种散列技术,C其中字符串的所有排列都具有相同的散列键.
例如abc,cab两者都应该有相同的键.
我曾经想过加入的ascii价值观和然后检查frequency of characters[重要,否则都abc与aad本来这是我们不希望相同的密钥.
但是,它看起来效率不高.
是否有更好的散列函数可以很好地解决冲突并且不会导致稀疏哈希表?
Java [ for strings]在内部使用哪种散列技术,不仅可以最大限度地减少冲突,而且操作[ insertion ,deletion, search]也足够快?
以下程序取得输入a sentence,an old word和一个new word.
目标是replace all the occurrences old word with the new word.
int countOccurrence(char* sen,char* word) //counts occurrences of old word
{
int count=0,i,k,len1,len2;
len1=strlen(sen);
len2=strlen(word);
for(i=0;i<len1-len2+1;)
{
k=0;
while(word[k] && sen[k+i]==word[k])
k++;
if(k==len2 && sen[k+i]==' ' || sen[k+i]=='\0')
{
count++;
i+=len2;
}
else ++i;
}
return count;
}
void replace(char* sen,char* oldword,char* newword)
{
int count,len1,len2,len3,i,top=-1,k;
char *ptr;
count=countOccurrence(sen,oldword);
if(!count) return;
len1=strlen(sen);
len2=strlen(oldword);
len3=strlen(newword);
ptr=(char*)malloc(sizeof(len1+count*(len3-len2)+1));
for(i=0;i<len1-len2+1;)
{
k=0; …Run Code Online (Sandbox Code Playgroud) 考虑一下belo代码片段:
class Base
{
public:
Base()
{
cout<<"Constructor"<<endl;
}
Base(const Base& rhs)
{
cout<<"Copy constructor"<<endl;
}
Base* Clone()
{
return new Base(*this); <--------------- 2
}
void* operator new(size_t size)
{
void* p=malloc(size);
cout<<"Inside new"<<endl;
return p;
}
};
int main()
{
Base b1; <------------ 1
Base* ptr=b1.Clone();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出为:
Constructor
Inside new
Copy constructor
Run Code Online (Sandbox Code Playgroud)
我一直听说第一个运算符new分配了一大块void类型然后新的运算符调用构造函数将该块转换为与LHS上的类型完全相同的类型.
那么,为什么没有为语句2调用构造函数呢?
我还想知道C++编译器为语句2采取的一系列精确操作.
请考虑以下代码段:
for(i=0;i<10;i+=2) // 1
for(i=0;i<2;i=i+2) // 2
Run Code Online (Sandbox Code Playgroud)
哪一个会更好用?
性能有什么不同吗?
下面的程序计算2加权到幂n而不使用任何循环,运行时递归或库函数[pow].
它使用TEMPLATE METAPROGRAMMING技术.
#include <iostream>
using namespace std;
template<int n> struct funStruct
{
enum { val = 2*funStruct<n-1>::val };
};
template<> struct funStruct<0>
{
enum { val = 1 };
};
int main()
{
cout << funStruct<8>::val << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在游荡,我可以用功能代替结构吗?