簡體   English   中英

C++:如何在class中使用向量function?

[英]C++: How to use a vector function in a class?

這道題 - Two Sum - 來自LeetCode ,函數(算法)代碼來自這里 我本科的時候用過C++。 但是我已經很久沒有使用它了。 我現在正在復習C++的數據結構。 為了復習C++的基礎知識,我搜索了很多很多網站。

這是我的代碼,我想實現我之前在那個問題中提到的算法。 但是我不知道main部分的代碼有什么問題。

int main()
{
    vector<int> numbers(4);
    int target;
    cout<<"input numbers"<<endl;
    for(int i=0; i<4; ++i)
    cin >> numbers[i];
    cout<<"target"<<endl;
    cin >> target;
    Solution solu;
    solu.twoSum(numbers, target);
    cout << solu.ans << endl;//No member named 'ans' in 'Solution'
}

我使用 Xcode 運行 C++。有人可以幫我解決這個問題嗎?

這里的解有錯誤, Control may reach end of non-void function 是因為 Xcode 編譯器還是代碼有錯誤?

請詳細解釋一下。 提前致謝。

第 1 點已經涵蓋,所以我正在回應第 2 點。首先是有問題的代碼:

vector<int> twoSum(vector<int>& nums, int target) 
{
    static int MAX = 99999;
    static int DELT = 49999;
    vector<int> ans;
    int x[MAX];
    memset(x, 0, sizeof(x));
    for (int i = 0; i < nums.size(); i++)
    {
        if (x[nums[i] + DELT])
        {
            ans.push_back(((i + 1) < x[nums[i] + DELT] ? 
                                      (i + 1) : x[nums[i] + DELT]));
            ans.push_back(((i + 1) > x[nums[i] + DELT] ? 
                                      (i + 1) : x[nums[i] + DELT]));
            return ans;
        }
        x[target - nums[i] + DELT] = i + 1;
    }
}

請注意,function 中唯一的return語句嵌套在for循環和if語句中。 這允許return語句永遠不會到達的可能性,因為沒有進入循環或if (x[nums[i] + DELT])永遠不會為真。 觸發此行為的一種簡單方法是輸入一個空的nums向量:

for (int i = 0; i < 0; i++)

永遠不會進入循環,也永遠不會到達 return 語句。 在這種情況下會返回什么? 沒有人知道,每次運行都可能不同。 也許程序會崩潰。 也許它會趴在你的沙發上,邊喝朗姆酒和伏特加邊看亞當桑德勒的馬拉松電影。

“哦,但這永遠不會發生。” 你說,也許不會。 但是當面對經過嚴格測試和強制執行的要求時,樂觀是一個糟糕的選擇,無論編譯器是否指出邏輯中的一個漏洞可能會導致很多痛苦和調試,而這個漏洞很容易被填補

vector<int> twoSum(vector<int>& nums, int target) 
{
    static int MAX = 99999;
    static int DELT = 49999;
    vector<int> ans;
    int x[MAX];
    memset(x, 0, sizeof(x));
    for (int i = 0; i < nums.size(); i++)
    {
        if (x[nums[i] + DELT])
        {
            ans.push_back(((i + 1) < x[nums[i] + DELT] ? 
                                      (i + 1) : x[nums[i] + DELT]));
            ans.push_back(((i + 1) > x[nums[i] + DELT] ? 
                                      (i + 1) : x[nums[i] + DELT]));
            break;
        }
        x[target - nums[i] + DELT] = i + 1;
    }
    return ans;
}

現在總是會返回ans ,但這允許返回的ans不包含或處理不正確的信息的可能性。 不是一個好的答案,因為twoSum可能不再殺死你的程序,但處理它的 output 可以。

bool twoSum(vector<int>& nums, int target, vector<int> &ans) 
{
    static int MAX = 99999;
    static int DELT = 49999;
    int x[MAX];
    memset(x, 0, sizeof(x));
    for (int i = 0; i < nums.size(); i++)
    {
        if (x[nums[i] + DELT])
        {
            ans.push_back(((i + 1) < x[nums[i] + DELT] ? 
                                      (i + 1) : x[nums[i] + DELT]));
            ans.push_back(((i + 1) > x[nums[i] + DELT] ? 
                                      (i + 1) : x[nums[i] + DELT]));
            return true;
        }
        x[target - nums[i] + DELT] = i + 1;
    }
    return false;
}

僅當達到退出條件時, twoSum返回true 如果返回false ,則ans的內容不可信任。 需要對算法做更多的工作以確保ans始終完整和正確,或者twoSum需要包裝在另一個 function 中以在執行前驗證輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM