小编Gre*_*lin的帖子

第一次后停止位置更新

我正在使用实现 LocationListener 类的服务。现在我想知道设备的当前位置(纬度,经度)。我使用了 requestLocationUpdates 方法,以便在位置更改时调用 onLocationChanged 方法。我希望 onLocationChanged 方法只应调用一次,即当我第一次获取位置时,不应再次调用它。我已经调用 stopSelf 方法来停止 onLocationChanged 方法中的服务。但我仍然收到位置更新。请帮我。

android

0
推荐指数
1
解决办法
1426
查看次数

c结构不允许静态成员,而c ++也是如此

今天,我对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)

见这里:http://ideone.com/2JRlF

为什么会这样?

c c++

0
推荐指数
1
解决办法
177
查看次数

C中字符串的哈希函数

我想实现一种散列技术,C其中字符串的所有排列都具有相同的散列键.
例如abc,cab两者都应该有相同的键.

我曾经想过加入的ascii价值观和然后检查frequency of characters[重要,否则都abc与aad本来这是我们不希望相同的密钥.
但是,它看起来效率不高.

是否有更好的散列函数可以很好地解决冲突并且不会导致稀疏哈希表?

Java [ for strings]在内部使用哪种散列技术,不仅可以最大限度地减少冲突,而且操作[ insertion ,deletion, search]也足够快?

c java

0
推荐指数
1
解决办法
250
查看次数

free()一块内存时获取错误

以下程序取得输入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)

c

0
推荐指数
1
解决办法
101
查看次数

operator new不调用构造函数

考虑一下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采取的一系列精确操作.

c++

-2
推荐指数
1
解决办法
692
查看次数

哪个更快:i = i + 2或i + = 2?

请考虑以下代码段:

for(i=0;i<10;i+=2) // 1
for(i=0;i<2;i=i+2) // 2
Run Code Online (Sandbox Code Playgroud)

哪一个会更好用?
性能有什么不同吗?

c performance

-3
推荐指数
2
解决办法
3万
查看次数

2上升到幂n没有任何循环或递归或库函数

下面的程序计算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)

我在游荡,我可以用功能代替结构吗?

c++ templates

-4
推荐指数
1
解决办法
2015
查看次数

标签 统计

c ×4

c++ ×3

android ×1

java ×1

performance ×1

templates ×1