使用带向量的find <pair <int,int =“”>&gt;

时间:2015-11-06 00:17:28

标签: c++ c++11 vector find

我遇到了以下问题:假设我有

onBindViewHolder

我想使用find来获取指向向量

中元素p的迭代器
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    //This will get the current position of the Information object from the Information array
    SubInformation current = data.get(position);

    holder.title.setText(current.getTitle());

    //add the click listener
    holder.root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = null;

            switch (getAdapterPosition()) {
                case 0:
                    //Toast.makeText(v.getContext(), "Default Case", Toast.LENGTH_SHORT).show();
                    //break;
                    intent = new Intent(context, AdminTeam.class);
                    //intent.putExtra("JSON Admin", ra.getItemCount());
                    break;

                case 1:
                    Toast.makeText(v.getContext(), "Default Case", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }

            //In order for an activity to begin, a context needs to be passed in
            //context.startActivity(new Intent(context, Introduction.class));
            context.startActivity(intent);
            //If the method is not called (Error handling to avoid NULL POINTER EXCEPTION ERROR)
            if (clickListener != null) {
                //Trigger the appropriate call. getPosition will get the latest position of the item clicked by the user
                clickListener.itemClicked(v, getAdapterPosition());
            }

        }
    });
}

class MyViewHolder extends RecyclerView.ViewHolder{

    TextView title;
    View root; //save the root view and add the click listener on it in OnBindViewHolder

    public MyViewHolder(View itemView) {
        super(itemView);
        root = itemView;

        //Here setting the id of the textview in the recycler view holder to be the list view from the custom_row xml
        title = (TextView) itemView.
                findViewById(R.id.listText);
    }


}

但它给了我错误

pair<int, int> p(1,2)
vector<pair<int, int>> vec;

我该怎么办?

1 个答案:

答案 0 :(得分:-1)

这就是我用过的,而且效果很好。

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

struct FindPair {
    FindPair (int first, int second)
    : m_first_value(first)
    , m_second_value(second) { }

    int m_first_value;
    int m_second_value;
    bool operator()
        ( const std::pair<int, int> &p ) {
            return (p.first == m_first_value && p.second == m_second_value);
    }
};


int main()
{

    std::vector< std::pair<int, int> > myVec;
    std::vector< std::pair<int, int> >::iterator it;

    myVec.push_back(std::make_pair(1,1));
    myVec.push_back(std::make_pair(1,2));

    it = std::find_if(myVec.begin(), myVec.end(), FindPair(1, 2));

    if (it != myVec.end())
    {
        // We Found it
        std::cout << "Matched Found on Current Iterator!" << std::endl;
        std::cout << "it.first: " << (*it).first << std::endl;
        std::cout << "it.second: " << (*it).second << std::endl;
    }
    else
    {
        std::cout << "Nothing Matched!" << std::endl;
    }

    return 0;
}

输出:

Matched Found on Current Iterator! 
it.first: 1 
it.second: 2