C++中“in”函数的等价物

120 阅读5分钟

Python中的“in”函数是一个非常有用的内置函数,允许访问变量中的所有元素。例如,以下Python代码检查用户输入是否为“yes”或“no”:

def main():
    y = ["Yes", "yes", "YES", ]
    n = ["No", "no", "NO"]
    print("yes or no?\n")
    response = raw_input()
    if response in y:
        print("Wonderful, your response was ", response, "\n")
        exit(0)
    if response in n:
        print("Alas, your response was ", response, "\n")
        exit(0)
    else:
        print("Message not read; please attempt again.\n")
        main()

main()

在C++标准库中,有没有类似于Python中“in”函数的功能?

2、解决方案 有几种方法可以实现类似于Python中“in”函数的功能:

  • std::find()函数: std::find()函数用于查找元素在容器中的位置。如果找到元素,则返回指向该元素的迭代器;否则,返回容器的end()迭代器。例如,以下C++代码检查用户输入是否为“yes”或“no”:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> y = {"Yes", "yes", "YES"};
    vector<string> n = {"No", "no", "NO"};

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (find(y.begin(), y.end(), response) != y.end()) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (find(n.begin(), n.end(), response) != n.end()) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
  • std::set::find()函数: std::set::find()函数用于查找元素在集合中的位置。如果找到元素,则返回指向该元素的迭代器;否则,返回集合的end()迭代器。例如,以下C++代码检查用户输入是否为“yes”或“no”:
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
    set<string> y = {"Yes", "yes", "YES"};
    set<string> n = {"No", "no", "NO"};

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (y.find(response) != y.end()) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (n.find(response) != n.end()) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
  • std::unordered_set::find()函数: std::unordered_set::find()函数用于查找元素在无序集合中的位置。如果找到元素,则返回指向该元素的迭代器;否则,返回集合的end()迭代器。例如,以下C++代码检查用户输入是否为“yes”或“no”:
#include <iostream>
#include <unordered_set>
#include <string>

using namespace std;

int main() {
    unordered_set<string> y = {"Yes", "yes", "YES"};
    unordered_set<string> n = {"No", "no", "NO"};

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (y.find(response) != y.end()) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (n.find(response) != n.end()) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
  • std::binary_search()函数: std::binary_search()函数用于查找元素在已排序容器中的位置。如果找到元素,则返回true;否则,返回false。例如,以下C++代码检查用户输入是否为“yes”或“no”:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    vector<string> y = {"Yes", "yes", "YES"};
    vector<string> n = {"No", "no", "NO"};

    sort(y.begin(), y.end());
    sort(n.begin(), n.end());

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (binary_search(y.begin(), y.end(), response)) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (binary_search(n.begin(), n.end(), response)) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
Python中的“in”函数是一个非常有用的内置函数,允许访问变量中的所有元素。例如,以下Python代码检查用户输入是否为“yes”或“no”:

```python
def main():
    y = ["Yes", "yes", "YES", ]
    n = ["No", "no", "NO"]
    print("yes or no?\n")
    response = raw_input()
    if response in y:
        print("Wonderful, your response was ", response, "\n")
        exit(0)
    if response in n:
        print("Alas, your response was ", response, "\n")
        exit(0)
    else:
        print("Message not read; please attempt again.\n")
        main()

main()
```
在C++标准库中,有没有类似于Python中“in”函数的功能?

2、**解决方案**
有几种方法可以实现类似于Python中“in”函数的功能:

* **std::find()函数:**
    std::find()函数用于查找元素在容器中的位置。如果找到元素,则返回指向该元素的迭代器;否则,返回容器的end()迭代器。例如,以下C++代码检查用户输入是否为“yes”或“no”:

```cpp
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    vector<string> y = {"Yes", "yes", "YES"};
    vector<string> n = {"No", "no", "NO"};

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (find(y.begin(), y.end(), response) != y.end()) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (find(n.begin(), n.end(), response) != n.end()) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
```

* **std::set::find()函数:**
    std::set::find()函数用于查找元素在集合中的位置。如果找到元素,则返回指向该元素的迭代器;否则,返回集合的end()迭代器。例如,以下C++代码检查用户输入是否为“yes”或“no”:

```cpp
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
    set<string> y = {"Yes", "yes", "YES"};
    set<string> n = {"No", "no", "NO"};

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (y.find(response) != y.end()) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (n.find(response) != n.end()) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
```

* **std::unordered_set::find()函数:**
    std::unordered_set::find()函数用于查找元素在无序集合中的位置。如果找到元素,则返回指向该元素的迭代器;否则,返回集合的end()迭代器。例如,以下C++代码检查用户输入是否为“yes”或“no”:

```cpp
#include <iostream>
#include <unordered_set>
#include <string>

using namespace std;

int main() {
    unordered_set<string> y = {"Yes", "yes", "YES"};
    unordered_set<string> n = {"No", "no", "NO"};

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (y.find(response) != y.end()) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (n.find(response) != n.end()) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
```

* **std::binary_search()函数:**
    std::binary_search()函数用于查找元素在已排序容器中的位置。如果找到元素,则返回true;否则,返回false。例如,以下C++代码检查用户输入是否为“yes”或“no”:

```cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    vector<string> y = {"Yes", "yes", "YES"};
    vector<string> n = {"No", "no", "NO"};

    sort(y.begin(), y.end());
    sort(n.begin(), n.end());

    cout << "yes or no?\n";
    string response;
    cin >> response;

    if (binary_search(y.begin(), y.end(), response)) {
        cout << "Wonderful, your response was " << response << "\n";
        return 0;
    } else if (binary_search(n.begin(), n.end(), response)) {
        cout << "Alas, your response was " << response << "\n";
        return 0;
    } else {
        cout << "Message not read; please attempt again.\n";
        return main();
    }
}
```
````