简体   繁体   English

向量指向另一个向量

[英]Vector point to another vector

What I have here is two arrays of different types that I'm converting to vectors. 我在这里有两个要转换为向量的不同类型的数组。

int ham_array[] = {32,71,12,45,26};

char word_array[] = {"cat", "bat", "green", "red", "taxi"};


vector < int > hamvector (ham_array, ham_array + 5);               

vector < char > wordvector(word_array, word_array + 5); 

I am going to call a sort function to sort the elements of ham_array from least to greatest. 我将调用排序函数,以从最小到最大对ham_array的元素进行排序。 At the same time, I would like the word_array to also get sorted the same way ham_vector gets sorted using references. 同时,我希望对word_array也进行排序,就像使用引用对ham_vector进行排序一样。

For example, 例如,

after I call sort(hamvector) 在我调用sort(hamvector)之后

ham_array[] = {12, 26, 32, 45, 71}

and sort(wordvector) 和排序(wordvector)

word_array[] = {"green", "taxi", "cat", "red", "bat"};

Is there an easy way to do this? 是否有捷径可寻?

Well for one thing, that would be char *word_array[] , the way you declared it would be a string. 好吧,那将是char *word_array[] ,您声明它的方式将是一个字符串。

Anyway the way to do this is you declare a structure to keep these things paired: 无论如何,执行此操作的方法是声明一个结构以使这些东西配对:

struct t {string name; int number;};
vector<t> list;
// fill in list

// comparer to compare two such structs
bool comparer(t &a, t &b) { return a.number>=b.number; }

// and to sort the list
sort(list.begin(), list.end(), comparer);

If by simple, you mean a more direct way then yes. 如果简单地说,您的意思是更直接的方法,那么可以。 The std::sort() does support sorting of raw arrays as well: std::sort()也支持原始数组的排序:

sort(word_array, word_array + 5, wordcmp);

As Blindy showed, you need a comparator function to tell sort how the ordering is suppose to be done for your list of words. 正如Blindy所展示的,您需要一个比较器函数来告诉sort ,假设您要对单词列表进行排序。 Otherwise you'll end up sorting by the memory address that the string resides at instead of by the letters in your string. 否则,您最终将根据字符串所在的内存地址而不是字符串中的字母进行排序。 Something like this should work: 这样的事情应该起作用:

int wordcmp(const char *lhs, const char *rhs)
{
    return strncmp(lhs, rhs, 256) < 0;
}

One other note, in practice you'll want to prefer std::vector over just raw pointer arrays since the latter isn't as safe. 另一个要注意的是,在实践中,您将要首选std::vector不是原始指针数组,因为后者并不安全。

I've tried to find a solution to a similar problem before and ultimately had to sort it manually. 我曾经尝试过找到类似问题的解决方案,最终不得不手动对其进行排序。 Another way I imagine you could do this would be to write a sorter functor that can somehow figure out, based on which string is being sorted, which integer is associated, and sort based on that. 我想您可以执行此操作的另一种方式是编写一个排序函子,该函子可以以某种方式根据正在排序的字符串,关联的整数以及基于此的排序来弄清楚。 This is terribly inefficient , so I would highly advise doing your own manual sorting using std::swap . 效率极低 ,因此,我强烈建议您使用std::swap手动排序。

#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

template<typename KeyType, typename ValueType>
class CMappedSorter
{
    std::map<KeyType, ValueType>* const m_Mappings;
public:
    CMappedSorter(std::map<KeyType, ValueType>* Mappings) : m_Mappings(Mappings)
    {

    }

    bool operator()(KeyType& LHS, KeyType& RHS)
    {
        const ValueType LHSSortingValue = m_Mappings->find(LHS)->second;
        const ValueType RHSSortingValue = m_Mappings->find(RHS)->second;
        return (LHSSortingValue < RHSSortingValue);
    }
};

int main(int argc, char* argv[])
{
    std::vector<int> Integers;
    std::vector<std::string> Strings;

    Integers.push_back(3);
    Integers.push_back(1);
    Integers.push_back(2);

    Strings.push_back("Apple");
    Strings.push_back("Banana");
    Strings.push_back("Cherry");

    std::map<std::string, int> Mappings;

    if(Integers.size() == Strings.size())
    {
        const unsigned int ElementCount = Strings.size();

        // Generate mappings.
        auto StringsIterator = Strings.begin();
        auto IntegersIterator = Integers.begin();
        for(unsigned int i = 0; i < ElementCount; ++i)
        {
            Mappings[*(StringsIterator)] = *(IntegersIterator);
            ++StringsIterator;
            ++IntegersIterator;
        }

        // Print out before sorting.
        std::cout << "Before Sorting" << std::endl;
        std::cout << "Int\tString" << std::endl;
        StringsIterator = Strings.begin();
        IntegersIterator = Integers.begin();
        for(unsigned int i = 0; i < ElementCount; ++i)
        {
            std::cout << *(IntegersIterator) << '\t' << *(StringsIterator) << std::endl;
            ++StringsIterator;
            ++IntegersIterator;
        }

        // Sort
        std::sort(Strings.begin(), Strings.end(), CMappedSorter<std::string, int>(&(Mappings)));
        std::sort(Integers.begin(), Integers.end());

        // Print out after sorting.
        std::cout << "After Sorting" << std::endl;
        std::cout << "Int\tString" << std::endl;
        StringsIterator = Strings.begin();
        IntegersIterator = Integers.begin();
        for(unsigned int i = 0; i < ElementCount; ++i)
        {
            std::cout << *(IntegersIterator) << '\t' << *(StringsIterator) << std::endl;
            ++StringsIterator;
            ++IntegersIterator;
        }
    }
    else
    {
        std::cout << "Error: Number of elements in each container are not equivalent." << std::endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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