English 中文(简体)
C+++ 在不明位置的情况下删除矢量中的精选值
原标题:C++ Deleting a specfic value in a vector without knowing location
if (find(visitable.begin(), visitable.end(), ourstack.returnTop())) { ... }

我想确定在矢量 visiable 中是否可以找到堆叠上方字符 ourstack 中的顶端字符。 如果有, 我想从 visiable 中删除此字符 。

我知道矢量使用erase ,但这需要该字符的具体位置(我不知道)。

这是我的""https://stackoverflow.com/ questions/10763233/algorithm-to-print-to-print-to-screen-paths-rough-a-text-maze">maze-path-probath-problection任务

还有,我的returnTop 给了我一个错误: class "std.stack<<char..." 没有成员返回Top 。 我宣布# 包含在我的程序顶部。 这里发生了什么?

提前感谢!

最佳回答

如果您正在使用 find , 那么您已经知道字符的位置 。 find 返回该字符所在位置的代理符, 或在找不到该字符时返回用作结尾的值 。

vector<?>::const_iterator iter =
    find(visitable.begin(), visitable.end(), ourstack.top());
if( iter != visitable.end() )
{
    visitable.erase( iter );
}

至于stack ,您正在寻找的函数是 top () 。标准 C++ 库不使用 columCased 标识符, 它更像是 Java 或 C# 的东西 。

问题回答

就像这样:

// Note assume C++0x notation for simplicity since I don t know the type of the template
auto character = ourstack.top();

auto iter = std::find(visitable.begin(), visitable.end(), character);
if (iter != visitable.end())
    visitable.erase(iter);

返回 Top 在堆叠类中不存在, 但顶部确实存在 。

或者,如果你想要一些通用的(和相当炫耀的方式)这样做:

// Assume type of vector and stack are the same
template <class T>
void TryRemoveCharacter(std::vector<T>& visitable, const std::stack<T>& ourStack)
{
    // Note, could have passed a ref to the character directly, which IMHO makes more sense
    const T& ourChar = ourStack.top();

    visitable.erase(std::remove_if(visitable.begin(), visitable.end(), [&ourChar](const T& character)
    {
        // Note, this will not work http://www.cplusplus.com/reference/algorithm/find/
        // says that std::find uses the operator== for comparisons but I doubt that
        // as compilers typically do not generate equal comparison operator.
        // See http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator
        // It s best to either overload the operator== to do a true comparison or
        // add a comparison method and invoke it here.
        return ourChar == character;
    }));
}

Note: this alternative way may not be a good idea for an assignment as your teacher will probably find suspicious that you introduce advanced C++ features (C++0x) all of a sudden.
However for intellectual curiosity it could work ;)

这里您如何使用 :

TryRemoveCharacter(visitable, ourstack);




相关问题
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?

热门标签