You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

STL vector求唯一值代码存在Bug,期望结果不符如何排查?

Bug Analysis and Fix for Getting Unique Values from a C++ Vector

Let's break down why your code isn't producing the expected unique values 1 2 4 6:

1. std::unique only removes adjacent duplicates

The std::unique algorithm doesn't eliminate all duplicate elements in the vector—it only removes consecutive duplicates. Since your combined vector (vecUniqueInputValues) isn't sorted, duplicates that aren't next to each other (like the scattered 1s, 2s, and 4s) won't be touched. For example, your combined vector starts as 2 4 4 2 4 1 4 1 6 4—non-adjacent duplicates fly under std::unique's radar without sorting first.

2. You're ignoring the return value of std::unique

When std::unique finishes processing, it returns an iterator pointing to the new end of the range with duplicates removed. Your code doesn't use this iterator to truncate the vector, so all the old, duplicate elements after this new end remain in the vector—this is why you see extra values in your output.

3. Your custom comparison function is redundant

Your uniqueCompressVectorCompareFunction does exactly what the default std::unique behavior uses (comparing values with ==), so you can omit it entirely without changing functionality.


Fixed Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

int main() {
    // Redirect input from file
    std::ifstream inputFile("testinput.txt");
    std::streambuf* pcurrRdBuf = std::cin.rdbuf();
    std::cin.rdbuf(inputFile.rdbuf());

    unsigned int uiNoOfFishes = 0;
    std::cin >> uiNoOfFishes;

    std::vector<unsigned int> vecOfLenOfFishes(uiNoOfFishes);
    std::vector<unsigned int> vecOfTimeHeadOfFishes(uiNoOfFishes);

    // Read fish lengths
    for(unsigned int uiIdx = 0; uiIdx < uiNoOfFishes; uiIdx++) {
        std::cin >> vecOfLenOfFishes[uiIdx];
    }

    // Read time heads
    for(unsigned int uiIdx = 0; uiIdx < uiNoOfFishes; uiIdx++) {
        std::cin >> vecOfTimeHeadOfFishes[uiIdx];
    }

    // Print original inputs
    std::cout << "Actual input length of fishes: " << std::endl;
    for(const auto& val : vecOfLenOfFishes) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    std::cout << "Actual input time head of fishes: " << std::endl;
    for(const auto& val : vecOfTimeHeadOfFishes) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    // Combine both vectors efficiently
    std::vector<unsigned int> vecUniqueInputValues;
    vecUniqueInputValues.reserve(uiNoOfFishes * 2);
    vecUniqueInputValues.insert(vecUniqueInputValues.end(), vecOfLenOfFishes.begin(), vecOfLenOfFishes.end());
    vecUniqueInputValues.insert(vecUniqueInputValues.end(), vecOfTimeHeadOfFishes.begin(), vecOfTimeHeadOfFishes.end());

    std::cout << "Combined input values: " << std::endl;
    for(const auto& val : vecUniqueInputValues) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    // Step 1: Sort to group duplicates together
    std::sort(vecUniqueInputValues.begin(), vecUniqueInputValues.end());

    // Step 2: Remove adjacent duplicates and truncate the vector
    auto lastUnique = std::unique(vecUniqueInputValues.begin(), vecUniqueInputValues.end());
    vecUniqueInputValues.erase(lastUnique, vecUniqueInputValues.end());

    // Print the final unique sorted values
    std::cout << "Compressed unique values sorted: " << std::endl;
    for(const auto& val : vecUniqueInputValues) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    // Restore original input buffer
    std::cin.rdbuf(pcurrRdBuf);

    return 0;
}

Expected Output

When run with your testinput.txt (content: 5 2 4 4 2 4 1 4 1 6 4), the output will be:

Actual input length of fishes: 
2 4 4 2 4 
Actual input time head of fishes: 
1 4 1 6 4 
Combined input values: 
2 4 4 2 4 1 4 1 6 4 
Compressed unique values sorted: 
1 2 4 6 

内容的提问来源于stack exchange,提问作者venkysmarty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:30:53