English 中文(简体)
包含独特内容的清单
原标题:List with unique elements

我需要一个集装箱,在那里:

  • when I add a new element that does not exist yet, it is added to the top of the list
  • when I add an element that already exists, it is not added and I I get its index in the list
  • once the element is inserted, it always has the same index and it can be accessed using this index

单单是不足的,因为我不能使用<代码>[index]的内容。 <代码>td:list 既不储存独一无二的内容。

我采用了一种混合解决办法,即<条码>、<条码>和<条码>>>,但可能有一些通用的标准模板?

我不想利用动力。 在每一次插入之后援引<代码>:unique是没有解决办法的。

问题回答

If you re using just a std::list (or std::vector, for that matter), you re not going to get around a linear search if you don t want to avoid duplicated, but you want to keep the original order. A simple std::vector based solution might be:

int
createIndex( std::vector<T>& references, T const& newValue )
{
    int results = std::find( references.begin(), references.end(), newValue )
                                    - references.begin();
    if ( results == references.size() ) {
        references.push_back( newValue );
    }
    return results;
}

或者,你可以使用<代码>std:map:

int
createIndex( std::map<T, int>& references, T const& newValue )
{
    st::map<T, int>::iterator results = references.find( newValue );
    if ( results == references.end() ) {
        results = references.insert(
                    std::make_pair( newValue, references.size() ) ).first;
    }
    return results->second;
}

(This supposes that T supports <. If not, you ll have to establish an ordering critera. Or use unordered_map and define a hash code for it.)





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签