C++;-检查一个字符串数组是否包含另一个字符串数组的所有元素 我最近一直在移植Python应用程序到C++,但是现在我无法理解如何移植一个特定的函数。下面是相应的Python代码: def foo(a, b): # Where `a' is a list of strings, as is `b' for x in a: if not x in b: return False return True
我希望有一个类似的功能:C++;-检查一个字符串数组是否包含另一个字符串数组的所有元素 我最近一直在移植Python应用程序到C++,但是现在我无法理解如何移植一个特定的函数。下面是相应的Python代码: def foo(a, b): # Where `a' is a list of strings, as is `b' for x in a: if not x in b: return False return True,c++,multidimensional-array,c++11,iteration,C++,Multidimensional Array,C++11,Iteration,我希望有一个类似的功能: bool foo (char* a[], char* b[]) { // ... } 最简单的方法是什么?我尝试过使用STL算法,但似乎无法让它们工作。例如,我现在有这样一个(使用glib类型): 我非常愿意使用C++ 11的特性。 C++版本中的排序不起作用,因为它是指针值的排序(与 STD::与其他所有的比较)。您可以通过提供适当的比较函子来解决这个问题。但是为什么你不在C++代码中使用 STD::String < /Cord>?Python字符串是实际
bool
foo (char* a[], char* b[])
{
// ...
}
最简单的方法是什么?我尝试过使用STL算法,但似乎无法让它们工作。例如,我现在有这样一个(使用glib类型):
<>我非常愿意使用C++ 11的特性。 C++版本中的排序不起作用,因为它是指针值的排序(与
sizeof(a)
不会给出数组的大小。它提供指向指针的指针的大小
因此,您有两种选择:快速而肮脏的即席解决方案是显式传递数组大小:
gboolean foo (gchar** a, int a_size, gchar** b, int b_size)
或者,更好的是,您可以使用向量而不是数组:
gboolean foo (const std::vector<gchar*>& a, const std::vector<gchar*>& b)
同样,对于std::includes
第二个问题是,您不操作字符串,而是操作字符指针。换句话说,std::sort
将按指针地址排序,而不是按字符串内容排序
同样,您有两个选择:
如果坚持使用字符指针而不是字符串,则可以为std::sort
指定自定义比较器(使用lambda,因为您在注释中提到可以使用它们)
简单,更干净,更安全。
< P>你的第一个问题与C++中的(不)数组的处理方式有关。数组是一种非常脆弱的阴影存在,如果你用一种有趣的方式观察它们,它们会被转换成指针。您的函数没有像您所期望的那样使用两个指向数组的指针。它需要两个指针对指针 换句话说,您将丢失有关数组大小的所有信息sizeof(a)
不会给出数组的大小。它提供指向指针的指针的大小
因此,您有两种选择:快速而肮脏的即席解决方案是显式传递数组大小:
gboolean foo (gchar** a, int a_size, gchar** b, int b_size)
或者,更好的是,您可以使用向量而不是数组:
gboolean foo (const std::vector<gchar*>& a, const std::vector<gchar*>& b)
同样,对于std::includes
第二个问题是,您不操作字符串,而是操作字符指针。换句话说,std::sort
将按指针地址排序,而不是按字符串内容排序
同样,您有两个选择:
如果坚持使用字符指针而不是字符串,则可以为std::sort
指定自定义比较器(使用lambda,因为您在注释中提到可以使用它们)
更简单、更干净、更安全。一个简单的python版本是:
bool foo(std::vector<std::string> const &a,std::vector<std::string> const &b) {
for(auto &s : a)
if(end(b) == std::find(begin(b),end(b),s))
return false;
return true;
}
bool-foo(std::vector const&a,std::vector const&b){
适用于(自动和s:a)
if(end(b)==std::find(begin(b)、end(b)、s))
返回false;
返回true;
}
结果表明,对输入进行排序非常慢。(在重复元素的情况下是错误的)即使是简单的函数通常也要快得多。这再次表明,过早优化是万恶之源 这是一个无序的_集版本,通常比原始版本快一些(或者是针对我测试的值/使用模式):
bool-foo(标准::向量常量和a,标准::无序集常量和b){
适用于(自动和s:a)
如果(b)计数小于1
返回false;
返回true;
}
另一方面,如果向量已经排序并且
b
相对较小(对于我来说小于200k左右),那么std::includes
非常快。因此,如果您关心速度,您只需针对实际处理的数据和使用模式进行优化即可。python版本的简单版本是:
bool foo(std::vector<std::string> const &a,std::vector<std::string> const &b) {
for(auto &s : a)
if(end(b) == std::find(begin(b),end(b),s))
return false;
return true;
}
bool-foo(std::vector const&a,std::vector const&b){
适用于(自动和s:a)
if(end(b)==std::find(begin(b)、end(b)、s))
返回false;
返回true;
}
结果表明,对输入进行排序非常慢。(在重复元素的情况下是错误的)即使是简单的函数通常也要快得多。这再次表明,过早优化是万恶之源 这是一个无序的_集版本,通常比原始版本快一些(或者是针对我测试的值/使用模式):
bool-foo(标准::向量常量和a,标准::无序集常量和b){
适用于(自动和s:a)
如果(b)计数小于1
返回false;
返回true;
}
另一方面,如果向量已经排序并且b
相对较小(对于我来说小于200k左右),那么std::includes
非常快。所以如果你在乎的话
std::sort(a.begin(), a.end());
std::sort(a.begin(), a.end(), [](gchar* lhs, gchar* rhs) { return strcmp(lhs, rhs) < 0; });
gboolean
foo (const std::vector<std::string>& a, const std::vector<std::string>& b)
{
gboolean result;
std::sort (a.begin(), a.end());
std::sort (b.begin(), b.end());
result = std::includes (b.begin(), b.end(),
a.begin(), a.end());
return result;
}
bool foo(std::vector<std::string> const &a,std::vector<std::string> const &b) {
for(auto &s : a)
if(end(b) == std::find(begin(b),end(b),s))
return false;
return true;
}
bool foo(std::vector<std::string> const& a,std::unordered_set<std::string> const& b) {
for(auto &s:a)
if(b.count(s) < 1)
return false;
return true;
}
gboolean foo(vector<string> const& a, unordered_set<string> const& b)