C ++ make_pair map找到第二个

时间:2018-06-01 22:54:50

标签: c++ stdmap

我有这样的地图(第一个和第二个参数都是唯一的):

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.Query;
import com.lorin.deadlineschmedline.R;
import com.lorin.deadlineschmedline.adapters.CourseAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity
        implements CourseAdapter.OnCourseSelectedListener,
        PopupMenu.OnMenuItemClickListener {

private FirebaseFirestore mFirestore;
private Query mQuery;
private FirebaseUser mCurrentUser;

private RecyclerView.Adapter mAdapter;

private static final String TAG = "MainActivity";
private static final int RC_SIGN_IN = 9001;
private static final int LIMIT = 50;

@BindView(R.id.rv_course_item_list)
RecyclerView mCourseRecyvlerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopup(view);
        }
    });

    // Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(true);

    // Firestore
    mFirestore = FirebaseFirestore.getInstance();
    mQuery = mFirestore.collection("courses").whereEqualTo("name", true).limit(LIMIT);
    mAdapter = new CourseAdapter(mQuery, this) {
        @Override
        protected void onDataChanged() {
            // Show/hide content if the query returns empty.
            if (getItemCount() == 0) {
                mCourseRecyvlerView.setVisibility(View.VISIBLE);
                //mEmptyView.setVisibility(View.VISIBLE);
            } else {
                mCourseRecyvlerView.setVisibility(View.VISIBLE);
                //mEmptyView.setVisibility(View.GONE);
            }
        }

        @Override
        protected void onError(FirebaseFirestoreException e) {
            // Show a snackbar on errors
            Snackbar.make(findViewById(android.R.id.content),
                    "Error: check logs for info.", Snackbar.LENGTH_LONG).show();
        }
    };

    mCourseRecyvlerView.setLayoutManager(new LinearLayoutManager(this));
    mCourseRecyvlerView.setAdapter(mAdapter);
}

private void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.add_component_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(this);
    popup.show();
}

@Override
public void onStart() {
    super.onStart();
    mAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();

    if (mAdapter != null) {
        mAdapter.stopListening();
    }
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.add_course:
            startActivity(new Intent(MainActivity.this, AddCourseActivity.class));
            return true;
        case R.id.add_course_component:
            startActivity(new Intent(MainActivity.this, AddCourseComponentActivity.class));
            return true;
        default:
            return true;
    }
}

@Override
public void onCourseSelected(DocumentSnapshot restaurant) {
    // Go to the details page for the selected activity
    Intent intent = new Intent(this, AddCourseActivity.class);
    startActivity(intent);

    //overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);
}

}

我有一个搜索器,它可以完美地用于地图的第一个值:

std::map<DWORD, DWORD> mapTest;
mapTest.insert(make_pair(1, 101));
mapTest.insert(make_pair(2, 102));
mapTest.insert(make_pair(3, 103));

但我需要另一种方法,它可以搜索第二个参数,看起来就像我展示的那样。如果可能的话,我不想使用for或while循环。据我所知,我可能需要一个find_if搜索,但我无法处理它。

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你可以使用lambdas以简单的方式完成它。

DWORD val = 103;
auto result = std::find_if(stMap.begin(), stMap.end(), [val](const auto& mo) {return mo.second == val; });

DWORD foundkey = result->first;

重要的是抓住你的输入

[val]