小编Luc*_*cas的帖子

迭代时从向量中删除项目?

我有一个向量,包含活动或非活动的项目.我希望此向量的大小对于性能问题保持较小,因此我希望从向量中删除已标记为非活动的项目.我在迭代时尝试这样做,但我收到错误"vector iterators incompatible".

vector<Orb>::iterator i = orbsList.begin();

    while(i != orbsList.end()) {
        bool isActive = (*i).active;

        if(!isActive) {
            orbsList.erase(i++);
        }
        else {
            // do something with *i
            ++i;
        }
    }
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl

52
推荐指数
5
解决办法
6万
查看次数

如何使用Python + Django显示当前时间?

我正在学习如何使用Python和Django生成一个打印出当前时间的小型webapp.我正在使用Google App Engine.

现在它只显示一个空白页面,但我希望它显示当前时间.我也想将功能映射到主页..不是/时间/.

from django.http import HttpResponse
from django.conf.urls.defaults import *
import datetime

# returns current time in html
def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

def main(): 
    # maps url to current_datetime func
    urlpatterns = patterns('',
        (r'^time/$', current_datetime),
    )

if __name__ == '__main__':
  main()
Run Code Online (Sandbox Code Playgroud)

python django google-app-engine

19
推荐指数
4
解决办法
5万
查看次数

Javascript Memoization解释?

从一本书中读取一个例子,有人可以解释当函数本身没有声明任何参数时,函数调用fibonacci如何接受参数'i'吗?

var fibonacci = (function () {
    var memo = [0, 1];
    var fib = function (n) {
        var result = memo[n];
        if (typeof result !== 'number') {
            result = fib(n - 1) + fib(n - 2);
            memo[n] = result;
        }
        return result;
    };
    return fib;
}());

for(var i = 0; i <= 10; i += 1) {
    document.writeln('// ' + i + ': ' + fibonacci(i));
}
Run Code Online (Sandbox Code Playgroud)

javascript

9
推荐指数
3
解决办法
5732
查看次数

处理execvp的参数数组?

当我调用时execvp,例如execvp(echo, b),其中b是命令a的参数数组,稍后更改此数组会影响先前执行的execvp调用吗?当我尝试调用execp(echo,b)时,它最终打印出来(null)而不是b中的内容.任何人都可以指出为什么以及我必须做什么才能正确传递参数?

c exec execvp

6
推荐指数
1
解决办法
3万
查看次数

-SIGCONT不继续暂停的过程吗?

kill -SIGCONT pid从另一个终端运行后,以下过程不会继续。

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("paused\n");
    pause();
    printf("continue\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望程序在发送信号并打印“ continue”之后继续执行。为什么这不按预期工作?

c command-line signals

5
推荐指数
1
解决办法
1万
查看次数

使用scanf读取带空格的字符串?

我希望以下内容要求输入,然后接受一个字符串(带空格),然后再做一次.但在输入第一个字符串后,它会重复输出"input $".

char command[80];

while(1)
    {
        printf("input$ ");
        scanf("%[^\n]", command);    
    }
Run Code Online (Sandbox Code Playgroud)

我的输出: nput$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$ input$^C

我想要的是:

input$ hi
input$ this can take spaces
input$
Run Code Online (Sandbox Code Playgroud)

c string scanf

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

让一个对象包含另一个相同类型的对象?

我想创建一个包含相同类型对象的对象.当您创建此类型的对象时,它会创建另一个创建另一个对象,依此类推,直到长度耗尽为止.但是,我得到临时警告的接收地址.我该如何解决这个问题?

class A {
   A(int len) {
      if(len > 0) {
         _a = & A(len-1);
      }
      else {
         _a = NULL;
      }
   }

   A* _a;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

C字符串变为空

我正在学习如何在C中获取参数,但是,当我使用以下输入运行下面的代码时,第一个变为null.

输入: ./a.out a b c d e f g h i j k

输出: (null) b c d e f g h i j k

#include <stdio.h>

    int main(int argc, char *argv[])
    {
        int i = 2, j = 0;
        char *foo = argv[1];
        char *bar[10];
        while(j < 10 && i < argc)
        {
            bar[j++] = argv[i++];
        }
        bar[j] = NULL;

        printf("%s ", foo);
        for(j = 0; bar[j] != NULL; j++)
        {
            printf("%s ", bar[j]);
        }
        printf("\n");

        return 0; …
Run Code Online (Sandbox Code Playgroud)

c argv command-line-arguments

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

为什么strtof总是输出0.0000?

strtof使用浮点数进行调用在我的本地计算机上运行正常,但在学校的服务器上,strtof始终返回0.000000.我查看是否有任何存储,errno因为0应该表示错误,但它表示成功.有谁知道为什么会这样?

这是代码.

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%f\n", strtof(argv[1],0));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c string floating-point

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

翻译句子的话?

我不明白我做错了什么.它看起来应该工作:

调用reverse_reverse("这个房子是蓝色的"); 应该首先打印出"这房子是蓝色的",然后"蓝色是房子这个"..

void reverse_reverse(char * str) {
    char temp;

    size_t len = strlen(str) - 1;
    size_t i;
    size_t k = len;

    for(i = 0; i < len; i++) {
        temp = str[k];
        str[k] = str[i];
        str[i] = temp;
        k--;

        if(k == (len / 2)) {
            break;
        }
    }

    cout << str << endl;

    i = 0;
    for(k = 0; k < len; k++) {
        if(str[k] == ' ') {
            size_t a = k;
            for(size_t b = i; b < …
Run Code Online (Sandbox Code Playgroud)

c c++ string

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

阅读Python中的每个单词和其余部分?

比如说我有字符串'20 30 i love you'.如何将20和30存储到自己的变量中,将其余的字符串存储到第三个变量中?

python string input

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

程序中的 '\222'

C 的新手,但运行此程序时出现以下错误:程序中的流浪 '\222'

unsigned long *new_intel_stack(unsigned long *sp, lwpfun func, void *arg)
{
    unsigned long *ebp;  

    push(sp,arg); /* argument */
    push(sp,lwp_exit); /* for lwp return purposes */
    push(sp,func); /* function's return address */
    push(sp,0x1abcdef1); /* bogus "saved" base pointer */
    ebp=sp;   /* remember sp from this point for later */
    push(sp,0x6c6f7453); /* push initial eax, ebx, ecx, edx, esi and edi */
    push(sp,0x66206e65); 
    push(sp,0x206d6f72); 
    push(sp,0x746e6957);
    push(sp,0x32207265);
    push(sp,0x21363030);
    push(sp,ebp);  /* push initial edp */

    return sp;
}
Run Code Online (Sandbox Code Playgroud)

c assembly

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