鍍金池/ 問答/ C++問答
尐飯團 回答

sort的第二個函數(shù)參數(shù)錯了

柚稚 回答

個人理解空間復(fù)雜度為O(1)的歸并排序是指內(nèi)存方面的空間復(fù)雜度,而忽略了堆棧里的O(logN)的空間復(fù)雜度(畢竟不在同一個空間)

//空間復(fù)雜度為O(1)的歸并排序
#include <iostream>
using namespace std;

void reverse_array(int a[], int n) {
    int i = 0;
    int j = n - 1;
    while (i < j) {
        swap(a[i], a[j]);
        ++i;
        --j;
    }
}

void exchange(int a[], int length, int length_left) {
    reverse_array(a, length_left);
    reverse_array(a + length_left, length - length_left);
    reverse_array(a, length);
}

void Merge(int a[], int begin, int mid, int end) {
    while (begin < mid && mid <= end) {
        int step = 0;
        while (begin < mid && a[begin] <= a[mid])
            ++begin;
        while (mid <= end && a[mid] <= a[begin]) {
            ++mid;
            ++step;
        }
        exchange(a + begin, mid - begin, mid - begin - step);
    }
}

void MergeCore(int a[], int left, int right) {
    if (left < right) {
        int mid = (left + right) / 2;
        MergeCore(a, left, mid);
        MergeCore(a, mid + 1, right);
        Merge(a, left, mid + 1, right);
    }
}

void MergeSort(int a[], int length) {
    if (a == NULL || length < 1)
        return;
    MergeCore(a, 0, length - 1);
}

int main() {
    int a[] = {1,0,2,9,3,8,4,7,6,5,11,99,22,88,11};
    int length = sizeof(a) / sizeof(int);
    MergeSort(a, length);
    
    for (int i = 0; i < length; i++)
        cout << a[i] << " ";
    cout << endl;
    return 0;
}
別逞強 回答

表達式"s"的類型是const char[],表達式 'a'的類型是char。

數(shù)組是不能進行內(nèi)置加法運算的。所以const char []會被轉(zhuǎn)換成const char *,這里的運算就變成了"指針+整型"(char是一種整型)。輸出空行的運行結(jié)果實際上是數(shù)組越界引起的。

String literal
Narrow multibyte string literal. The type of an unprefixed string literal is const char[]

Additive operators
addition. For the built-in operator, lhs and rhs must be one of the following: Both have arithmetic or unscoped enumeration type. In this case, the usual arithmetic conversions are performed on both operands and determine the type of the result. One is a pointer to complete object type, the other has integral or unscoped enumeration type. In this case, the result type has the type of the pointer

Array-to-pointer decay
There is an implicit conversion from lvalues and rvalues of array type to rvalues of pointer type: it constructs a pointer to the first element of an array. This conversion is used whenever arrays appear in context where arrays are not expected, but pointers are.

逗婦乳 回答
? super Child:參數(shù)類型必須是Child或者Child的父類,但Child父類無法確定,所以不能add

只能add Child或者Child的子類,因為Child的子類肯定也屬于Child型的。

舊螢火 回答

p指向int的信息是給編譯器進行靜態(tài)檢查用的,在編譯后的程序里是沒有記錄p指向的是一個int
圖片描述

如圖所示,我上面兩個函數(shù),雖然指針類型不一樣,但編譯結(jié)果是一樣的
https://godbolt.org/
你可以在這個網(wǎng)站里在線把C編譯成匯編

野橘 回答
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//** 聲明結(jié)構(gòu)
//** @result 返回對稱數(shù)數(shù)組
//** @length 數(shù)組的長度
struct Symmetrical {
    int *result;
    int length;
};

typedef struct Symmetrical Symmetrical;

Symmetrical *findSymmetrical() {
    //為結(jié)果分配內(nèi)存,最大可能為所有數(shù)都是對稱數(shù),即 sizeof(int)*1993
    int *result = (int *)malloc(sizeof(int)*1993);
    int length = 0;
    //數(shù)字轉(zhuǎn)換為字符串后存放的數(shù)組
    char *numStr = (char *)malloc(sizeof(char)*4);
    
    for (int i = 1; i <= 1993; i++) {
        //如果小于10,一定為對稱數(shù)
        if (i < 10) result[length++] = i;
        else {
            int is_symmetrical = 1;
            // 轉(zhuǎn)換數(shù)字到字符串,itoa是非標準函數(shù),可以用sprintf代替
            sprintf(numStr, "%d", i);
            // itoa(i, numStr, 10);
            int num_length = strlen(numStr);
            //對每一位進行對比
            for (int j = 0; j < num_length / 2; j++) {
                //如果正數(shù)第j位和倒數(shù)第j位不一樣,則不為對稱數(shù),結(jié)束循環(huán)
                if (numStr[j] != numStr[num_length - j - 1]) {
                    is_symmetrical = 0;
                    break;
                }
            }
            
            //如果是對稱數(shù),存到結(jié)果數(shù)組里
            if (is_symmetrical) result[length++] = i;
        }
    }
    free(numStr);
    
    //創(chuàng)建對稱數(shù)結(jié)構(gòu),用于同時傳遞數(shù)組和數(shù)組長度
    Symmetrical *symmetrical = (Symmetrical *)malloc(sizeof(Symmetrical));
    symmetrical->result = result;
    symmetrical->length = length;
    return symmetrical;
}

int main() {
    Symmetrical *result = findSymmetrical();
    
    //打印
    for (int i = 0; i < result->length; i++) {
        printf("%d ", result->result[i]);
    }
    free(result->result);
    free(result);
}

答案非最優(yōu)解,但思路是這樣。

久愛她 回答

1.animal要運行一次才會執(zhí)行函數(shù)體的內(nèi)容

2.直接調(diào)用animal(),this在瀏覽器中指向window,在node中指向global

3.因此要向執(zhí)行eat需要這么寫:

animal()

// equal to window.eat() / global.eat()
eat()

4.var一個實例的方法是把animal當做構(gòu)造器函數(shù)使用,new的過程中會執(zhí)行函數(shù)體內(nèi)容,并返回this,可以這么寫:

// this指向cat
var cat = new animal()

cat.eat()
失魂人 回答

1) 看下這個https://benchmarksgame-team.p...,這個基本上Cpp無一落敗
2) 除了-O2外,還有-O3,還有其他的編譯參數(shù),請參見GCC手冊
3) JVM默認是用空間換時間的,所以這么對比不是很適合

你的代碼,我在未優(yōu)化一行代碼的情況下,使用-O3來測試(實際上release都是-O3),運算3次都比Java快,這中間還包括加載程序啟動的時間,怎么得出結(jié)論會比java慢呢。

當然,C++對程序員要求很高,不了解內(nèi)存模型、編譯原理什么的,是很難寫出高質(zhì)量的C++的,在這一點上,java就好很多

最近買了一個樹莓派3B+,特意跑了下這個程序,從性能上看,C++比java在Arm上略快,優(yōu)勢不明顯,另外寫了一個rust版本的代碼,算法上未優(yōu)化,性能跟C++接近,在release情況下比Java略快。

fn main() {
    let input_num=100001;
    let mut pp_count =0;
    for  each in 2..input_num {
        let mut factorization_lst=0;
        for  factor in 1..each+1 {
            if each%factor==0 &&!(factor>each/factor) {
                factorization_lst += 1;
            }
        }
        if factorization_lst==1
        {
            let mut antitone =0;
            let mut each_cpy =each;
            while each_cpy != 0
            {
                antitone=antitone*10+each_cpy%10;
                each_cpy/=10;
            }
            if antitone==each
            {
               pp_count += 1;
               println!("{}:{}", pp_count, each);
            }
        }
    }
}

從CPU上來,基本上在運行期這3個程序都是跑滿單核的(樹莓派3B+有4個core),但內(nèi)存上來看,C++和rust有明顯優(yōu)勢,大概為java版本的1/10.
這個測試從測試結(jié)果來看,這幾個語言的運行性能差異沒那么大,分析了下有幾個原因
1) 對于int數(shù)據(jù)類型,在java C++ rust里都是原生類型,原生的在計算時差別不大,對于java來說,如果是Integer可能有性能損耗。
2) 只跑了一個核,沒有多核之間的數(shù)據(jù)傳遞等
3) 沒有用到遞歸、值傳遞、引用傳遞、值拷貝等特性,差異不大。

結(jié)論: java是一個性價比比較好的語言,運行性能上或許不是最優(yōu),但開發(fā)效率很好,不像其他的語言要考慮跨平臺移植問題、編譯參數(shù)問題等。
PS 未來看好rust

涼心人 回答

ios打包需要xcode這個工具來打包,先用cordova生成文件夾,然后把ios這個文件夾移動到mac,用xcode這個工具打開文件夾,https://www.jianshu.com/p/019... 可以看一下這篇文章,寫的挺詳細的

祉小皓 回答

C語言中自加++自減--運算的數(shù)據(jù)類型可以是Float和Double!
自加,自減運算符是系統(tǒng)提供的一種方便的數(shù)學(xué)運算書寫格式,可操作的變量類型包括:整型、浮點型、單字符型、指針類型
如:
int a=0; a++;
double b=2.1; b++; //b++后,b=3.1
char ch='a' ; ch++; //ch++后,ch='b' ;
int a[]={1,2,3}, *p=a; p++; //開始p指向數(shù)組第一個元素1,p++后,p指向下一個元素2

風清揚 回答

因為你進入后沒有再離開(next());
當然也就沒有再執(zhí)行beforeRouteLeave的機會了

紓惘 回答

這明顯是找不到share.js這個文件,你代碼下載下來放的路徑是啥

浪婳 回答

首先, 你需要展示自己在解決這個問題的過程中做出了哪些嘗試, 搜了哪些東西, 得出些什么結(jié)論. 而不是這樣就扔個代碼.
先把這篇帖子讀了: https://stackoverflow.com/que...

為了充實下此答案, 稍微結(jié)合下你的問題說下, 雖然沒什么意義, 因為那篇帖子說的很好了, 不過沒有引用標準,
語言律師可能不是很喜歡, 所以我再補充些標準里的. (這是符合stackexchange的規(guī)則的, 因為我給出了原鏈):

雖然有了前置聲明, 但是編譯器并不知道Fsm的大小, 成員, 方法. 它此時被稱作incomplete type, 它是不能被解引用的(operator->其實也是解引用符), 所以你用vs的話雙擊error會調(diào)到幾行解引用的地方, compile time error了.

clipboard.png

病癮 回答
mongo --host 'mongodb://10.0.1.59:27017'
不舍棄 回答

new formData可以傳入一個form標簽進去,form標簽內(nèi)所有的攜帶name屬性的表單元素會被認為是formItem。

表單元素包含下列(可能還有其他的,但是不太常用了,2333):

  1. input
  2. textarea

如果你確定你數(shù)據(jù)的來源是一個div,那么很抱歉,直接new FormData是不能夠得到你想要的結(jié)果的
需要你自己在后邊進行append的操作:

formData.append('content', document.querySelector(".ql-editor").innerHTML)