简体   繁体   中英

no match for ‘operator[]' when trying to simplify code into range-based for-loop for move semantics

I'm trying to reduce and simplify the size of my code, but encountered this error:

BucketSort.cpp: In member function ‘void BucketSort::sort(unsigned int)’:
BucketSort.cpp:118:34: error: no match for ‘operator[]’ (operand types are ‘std::vector<unsigned int> [10]’ and ‘std::vector<unsigned int>’)
         std::move(std::begin(vecs[i]), std::end(vecs[i]), std::back_inserter(numbersToSort));
                                  ^
BucketSort.cpp:118:53: error: no match for ‘operator[]’ (operand types are ‘std::vector<unsigned int> [10]’ and ‘std::vector<unsigned int>’)
         std::move(std::begin(vecs[i]), std::end(vecs[i]), std::back_inserter(numbersToSort));
                                                     ^
make: *** [BucketSort.o] Error 1
Ivans-MacBook-Pro:CS6771A5-ParallelBucketSort ivanteong$

I'm trying to simplify from:

numbersToSort = std::move(vecs[0]); // bucket containing 0 (handle case where there is a number that is 0)
    std::move(std::begin(vecs[1]), std::end(vecs[1]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[2]), std::end(vecs[2]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[3]), std::end(vecs[3]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[4]), std::end(vecs[4]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[5]), std::end(vecs[5]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[6]), std::end(vecs[6]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[7]), std::end(vecs[7]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[8]), std::end(vecs[8]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[9]), std::end(vecs[9]), std::back_inserter(numbersToSort));

to a range-based for-loop:

for (auto i : vecs) {
    std::move(
        std::begin(vecs[i]),
        std::end(vecs[i]),
        std::back_inserter(numbersToSort)
    );
}

Declarations in my .h file are:

std::vector<unsigned int> numbersToSort;
std::vector<unsigned int> vecs[10] = {
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>(),
    std::vector<unsigned int>()
};

Does anyone know what is wrong?

I think what you want is more:

for (auto& vec : vecs) {
    std::move(
        std::begin(vec),
        std::end(vec),
        std::back_inserter(numbersToSort)
    );
}

The range-based for-loop doesn't expand an index but a true variable copying, or, using auto& instead of auto , referencing the content of the given container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM