C ++中的谓词

时间:2018-09-15 23:53:28

标签: c++

C ++ STL算法中的谓词参数在模板中定义为类型参数。按照我的理解,谓词可以是两件事之一-

1)一个带有重载bool的operator()的结构。可以将其用作谓词的模板参数中的类型

2)一个返回布尔值的函数。这是模板中谓词的类型参数吗?

1 个答案:

答案 0 :(得分:2)

让我们检查一个简单的定义:

package com.example.nodemcu.aoatechapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.support.v7.app.AlertDialog;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URISyntaxException;

public class BedRoom1Activity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{

final Context context = this;
private Switch switch1, switch2, switch3, switch4;
String address = "aoatech.ddns.net";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bed_room1);

    switch1 = (Switch)findViewById(R.id.switch1);
    switch1.setOnCheckedChangeListener(this);
    switch2 = (Switch)findViewById(R.id.switch2);
    switch2.setOnCheckedChangeListener(this);
    switch3 = (Switch)findViewById(R.id.switch3);
    switch3.setOnCheckedChangeListener(this);
    switch4 = (Switch)findViewById(R.id.switch4);
    switch4.setOnCheckedChangeListener(this);
}

public void onCheckedChanged (CompoundButton compoundButton, boolean b){
    String serverAdress = address + ":3000";
    String ledStatus;
    if (switch1.isChecked()) {
        ledStatus = "1";
        Toast.makeText(BedRoom1Activity.this, "Light 1 is now Turned On...", Toast.LENGTH_SHORT).show();
        HttpRequestTask1 R1 = new HttpRequestTask1(serverAdress,(BedRoom1Activity) context);
        R1.execute(ledStatus);
    }
    else if (!switch1.isChecked()) {
        ledStatus = "0";
        Toast.makeText(BedRoom1Activity.this, "Light 1 is now Turned Off...", Toast.LENGTH_SHORT).show();
        HttpRequestTask1 R1 = new HttpRequestTask1(serverAdress,(BedRoom1Activity) context);
        R1.execute(ledStatus);
    }
    else if (switch2.isChecked()) {
        ledStatus = "1";
        Toast.makeText(BedRoom1Activity.this, "Light 2 is now Turned On...", Toast.LENGTH_SHORT).show();
        HttpRequestTask2 R2 = new HttpRequestTask2(serverAdress,(BedRoom1Activity) context);
        R2.execute(ledStatus);
    }
    else if (!switch2.isChecked()) {
        ledStatus = "0";
        Toast.makeText(BedRoom1Activity.this, "Light 2 is now Turned Off...", Toast.LENGTH_SHORT).show();
        HttpRequestTask2 R2 = new HttpRequestTask2(serverAdress,(BedRoom1Activity) context);
        R2.execute(ledStatus);
    }
    else if (switch3.isChecked()) {
        ledStatus = "1";
        Toast.makeText(BedRoom1Activity.this, "Fan is now Turned On...", Toast.LENGTH_SHORT).show();
        HttpRequestTask3 R3 = new HttpRequestTask3(serverAdress,(BedRoom1Activity) context);
        R3.execute(ledStatus);
    }
    else if (!switch3.isChecked()) {
        ledStatus = "0";
        Toast.makeText(BedRoom1Activity.this, "Fan is now Turned Off...", Toast.LENGTH_SHORT).show();
        HttpRequestTask3 R3 = new HttpRequestTask3(serverAdress,(BedRoom1Activity) context);
        R3.execute(ledStatus);
    }
    else if (switch4.isChecked()) {
        ledStatus = "1";
        Toast.makeText(BedRoom1Activity.this, "Socket is now Turned On...", Toast.LENGTH_SHORT).show();
        HttpRequestTask4 R4 = new HttpRequestTask4(serverAdress,(BedRoom1Activity) context);
        R4.execute(ledStatus);
    }
    else if (!switch4.isChecked()) {
        ledStatus = "0";
        Toast.makeText(BedRoom1Activity.this, "Socket is now Turned Off...", Toast.LENGTH_SHORT).show();
        HttpRequestTask4 R4 = new HttpRequestTask4(serverAdress,(BedRoom1Activity) context);
        R4.execute(ledStatus);
    }
}
private static class HttpRequestTask1 extends AsyncTask<String, Void, String> {

    private String serverAdress;
    private String serverResponse = "";
    AlertDialog dialog;
    private WeakReference<BedRoom1Activity> activityReference;
    private HttpRequestTask1(String serverAdress, BedRoom1Activity context) {
        this.serverAdress = serverAdress;
        this.activityReference = new WeakReference<>(context);
        dialog = new AlertDialog.Builder(activityReference.get())
                .setTitle("HTTP Response from Ip Address:")
                .setCancelable(true)
                .create();
    }
    @Override
    protected String doInBackground(String... params) {
        dialog.setMessage("Data sent , waiting response from server...");
        if (!dialog.isShowing()) {
            dialog.show();
        }
        String val = params[0];
        final String url = "http://" + serverAdress + "/light1/" + val;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet();
            getRequest.setURI(new URI(url));
            HttpResponse response = client.execute(getRequest);
            InputStream inputStream = null;
            inputStream = response.getEntity().getContent();
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(inputStream));
            serverResponse = bufferedReader.readLine();
            inputStream.close();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        }
        return serverResponse;
    }
    @Override
    protected void onPostExecute(String s) {
        dialog.setMessage(serverResponse);
        if (!dialog.isShowing())
            dialog.show();
    }
    @Override
    protected void onPreExecute() {
        dialog.setMessage("Sending data to server, please wait...");
        if (!dialog.isShowing())
            dialog.show();
    }
}

private static class HttpRequestTask2 extends AsyncTask<String, Void, String> {

    private String serverAdress;
    private String serverResponse = "";
    AlertDialog dialog;
    private WeakReference<BedRoom1Activity> activityReference;
    private HttpRequestTask2(String serverAdress, BedRoom1Activity context) {
        this.serverAdress = serverAdress;
        this.activityReference = new WeakReference<>(context);
        dialog = new AlertDialog.Builder(activityReference.get())
                .setTitle("HTTP Response from Ip Address:")
                .setCancelable(true)
                .create();
    }
    @Override
    protected String doInBackground(String... params) {
        dialog.setMessage("Data sent , waiting response from server...");
        if (!dialog.isShowing()) {
            dialog.show();
        }
        String val = params[0];
        final String url = "http://" + serverAdress + "/light2/" + val;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet();
            getRequest.setURI(new URI(url));
            HttpResponse response = client.execute(getRequest);
            InputStream inputStream = null;
            inputStream = response.getEntity().getContent();
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(inputStream));
            serverResponse = bufferedReader.readLine();
            inputStream.close();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        }
        return serverResponse;
    }
    @Override
    protected void onPostExecute(String s) {
        dialog.setMessage(serverResponse);
        if (!dialog.isShowing())
            dialog.show();
    }
    @Override
    protected void onPreExecute() {
        dialog.setMessage("Sending data to server, please wait...");
        if (!dialog.isShowing())
            dialog.show();
    }
}

private static class HttpRequestTask3 extends AsyncTask<String, Void, String> {

    private String serverAdress;
    private String serverResponse = "";
    AlertDialog dialog;
    private WeakReference<BedRoom1Activity> activityReference;
    private HttpRequestTask3(String serverAdress, BedRoom1Activity context) {
        this.serverAdress = serverAdress;
        this.activityReference = new WeakReference<>(context);
        dialog = new AlertDialog.Builder(activityReference.get())
                .setTitle("HTTP Response from Ip Address:")
                .setCancelable(true)
                .create();
    }
    @Override
    protected String doInBackground(String... params) {
        dialog.setMessage("Data sent , waiting response from server...");
        if (!dialog.isShowing()) {
            dialog.show();
        }
        String val = params[0];
        final String url = "http://" + serverAdress + "/fan/" + val;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet();
            getRequest.setURI(new URI(url));
            HttpResponse response = client.execute(getRequest);
            InputStream inputStream = null;
            inputStream = response.getEntity().getContent();
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(inputStream));
            serverResponse = bufferedReader.readLine();
            inputStream.close();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        }
        return serverResponse;
    }
    @Override
    protected void onPostExecute(String s) {
        dialog.setMessage(serverResponse);
        if (!dialog.isShowing())
            dialog.show();
    }
    @Override
    protected void onPreExecute() {
        dialog.setMessage("Sending data to server, please wait...");
        if (!dialog.isShowing())
            dialog.show();
    }
}

private static class HttpRequestTask4 extends AsyncTask<String, Void, String> {

    private String serverAdress;
    private String serverResponse = "";
    AlertDialog dialog;
    private WeakReference<BedRoom1Activity> activityReference;
    private HttpRequestTask4(String serverAdress, BedRoom1Activity context) {
        this.serverAdress = serverAdress;
        this.activityReference = new WeakReference<>(context);
        dialog = new AlertDialog.Builder(activityReference.get())
                .setTitle("HTTP Response from Ip Address:")
                .setCancelable(true)
                .create();
    }
    @Override
    protected String doInBackground(String... params) {
        dialog.setMessage("Data sent , waiting response from server...");
        if (!dialog.isShowing()) {
            dialog.show();
        }
        String val = params[0];
        final String url = "http://" + serverAdress + "/socket/" + val;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet();
            getRequest.setURI(new URI(url));
            HttpResponse response = client.execute(getRequest);
            InputStream inputStream = null;
            inputStream = response.getEntity().getContent();
            BufferedReader bufferedReader =
                    new BufferedReader(new InputStreamReader(inputStream));
            serverResponse = bufferedReader.readLine();
            inputStream.close();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        }
        return serverResponse;
    }
    @Override
    protected void onPostExecute(String s) {
        dialog.setMessage(serverResponse);
        if (!dialog.isShowing())
            dialog.show();
    }
    @Override
    protected void onPreExecute() {
        dialog.setMessage("Sending data to server, please wait...");
        if (!dialog.isShowing())
            dialog.show();
    }
}
}

运行此代码时,会出现错误。错误消息:

  

template <typename T> struct TD; bool is_odd(const int x) { return x % 2 != 0; } template<typename T> void foo(T arg){ TD<T> type_displayer; } int main() { foo(is_odd); } type_displayer”的类型不完整

这是检查派生类型(来自TD<bool (*)(int)>参数的T)时最喜欢的“ hack”。您可以清楚地看到我是如何在foo中将函数传递给foo的,但是推导的类型是main,它是返回<bool (*)(int)>的函数的指针。并以bool作为参数

仅此而已就是将函数作为参数传递给模板函数。

有关功能指针的参考,请参见this questionthis tutorial