C ++:lambda函数和捕获列表的问题

时间:2015-11-29 15:48:50

标签: c++ lambda

我有以下功能:

double mn = 2.0;
double mx = 5.0;
auto isBetween = [&,mx,mn](double y) -> bool{
    if (mn<y<mx) {
        return true;
    } else {
        return false;
    }
};

但是,当我调试时,我传递给double y的所有值都返回true。

我有什么遗失的吗?

3 个答案:

答案 0 :(得分:4)

您的测试if (mn<y<mx)不是您想要的。

您需要这样的内容:if (mn < y && y < mx)

答案 1 :(得分:2)

在C ++中,(mn < y < mx)运算符不能像数学中那样被链接。在表达式(mn < y)中,1 < mx的计算结果为1(真)或0(假),从那里表达式等同于0 < mxpublic class PathfinderUpdate extends Fragment { ConnectionClass connectionClass; EditText edtproname, edtprodesc; Button btnadd,btnupdate,btndelete,btnrefresh; ProgressBar pbbar; ListView lstpro; String pathid; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.update_pathfinder, container, false); connectionClass = new ConnectionClass(); btnupdate = (Button) rootView.findViewById(R.id.btnupdate); lstpro = (ListView) rootView.findViewById(R.id.lstproducts); btnrefresh = (Button) rootView.findViewById(R.id.btnrefresh); pathid = ""; FillList fillList = new FillList(); fillList.execute(""); btnrefresh.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { FillList Fill = new FillList(); Fill.execute(""); } }); return rootView; } @Override public void onResume(){ super.onResume(); FillList Fill = new FillList(); Fill.execute(""); } public class FillList extends AsyncTask<String, String, String> { String z = ""; List<Map<String, String>> prolist = new ArrayList<Map<String, String>>(); @Override protected void onPreExecute() { //old pbbar } @Override protected void onPostExecute(String r) { String[] from = { "pathfinder_id", "pathfinder_name"}; int[] views = { R.id.lblproid, R.id.lblproname }; final SimpleAdapter ADA = new SimpleAdapter(getActivity(), prolist, R.layout.lsttemplate, from,views); lstpro.setAdapter(ADA); lstpro.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { @SuppressWarnings("unchecked") HashMap<String, Object> obj = (HashMap<String, Object>) ADA .getItem(arg2); pathid = (String) obj.get("pathfinder_id"); String idea_name = (String) obj.get("pathfinder_name"); String benefit_eqv = (String) obj.get("pathfinder_eqv"); String quickwin = (String) obj.get("pathfinder_quick"); String observe = (String) obj.get("pathfinder_obs"); String ideaId = (String) obj.get("pathfinder_idea_id"); String BenefitId = (String) obj.get("pathfinder_benefit"); String closure = (String) obj.get("pathfinder_closure"); Integer ideaIdMain = Integer.parseInt(ideaId); Integer benefitIdMain = Integer.parseInt(BenefitId); Integer pathfinderId = Integer.parseInt(pathid); Double benefiteqv = Double.parseDouble(benefit_eqv); Bundle bundle = new Bundle(); bundle.putString("id2", pathid); bundle.putString("name", idea_name); bundle.putDouble("eqv", benefiteqv); bundle.putString("quick", quickwin); bundle.putString("observation", observe); bundle.putInt("idea_id", ideaIdMain); bundle.putInt("benefit_id", benefitIdMain); bundle.putString("closure", closure); bundle.putInt("id", pathfinderId); Intent updateMain = new Intent(getActivity(), PathfinderUpdateMain.class); updateMain.putExtras(bundle); startActivity(updateMain); // qty.setText(qtys); } }); } @Override protected String doInBackground(String... params) { try { Connection con = connectionClass.CONN(); if (con == null) { z = "Error in connection with SQL server"; } else { String query = "select * from pathfinder ORDER BY pathfinder_id ASC"; PreparedStatement ps = con.prepareStatement(query); ResultSet rs = ps.executeQuery(); ArrayList<String> data1 = new ArrayList<String>(); while (rs.next()) { Map<String, String> datanum = new HashMap<String, String>(); datanum.put("pathfinder_id", rs.getString("pathfinder_id")); datanum.put("pathfinder_name", rs.getString("pathfinder_name")); datanum.put("pathfinder_status", rs.getString("pathfinder_status")); datanum.put("pathfinder_eqv", rs.getString("pathfinder_potential_eqv")); datanum.put("pathfinder_obs", rs.getString("pathfinder_observation")); datanum.put("pathfinder_quick", rs.getString("pathfinder_quickwin")); datanum.put("pathfinder_idea_id", rs.getString("idea_id")); datanum.put("pathfinder_benefit", rs.getString("benefit_id")); datanum.put("pathfinder_closure", rs.getString("pathfinder_target_closure")); prolist.add(datanum); } z = "Success"; } } catch (Exception ex) { z = "Error retrieving data from table"; Log.e("MYAPP", "exception", ex); } return z; } } }

答案 2 :(得分:1)

您的问题在(mn<y<mx),可以理解为

(mn < y) < mx

这意味着,首先评估mn < y,这会导致bool类型的结果,并且是turefalse。最后将结果与mx进行比较。

您尝试做的是检查mn < y y < mx,必须表达的内容如下:

if (mn<y && y<mx)