小编tsi*_*iki的帖子

尝试插入集合(c ++)时,'operator <'不匹配?

我正在使用gcc 4.3.3尝试编译以下代码:

struct testStruct {  
int x;  
int y;  
bool operator<(testStruct &other) { return x < other.x; }  
testStruct(int x_, int y_) {  
    x = x_;  
    y = y_;  
}  
};  


int main() {
multiset<testStruct> setti;  
setti.insert(testStruct(10,10));  
return 0;  
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:/
usr/include/c++/4.4/bits/stl_function.h|230| error:'__x <__y'中'operator <'不匹配
我怀疑我没有对运算符进行重载,因为它应该做,但我无法确切地指出确切的问题.我在这做错了什么?

c++ stl set

9
推荐指数
1
解决办法
3900
查看次数

具有缓慢插入性能的多索引

我有以下代码(主要遵循这里的第一个示例:http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/examples.html)).由于某种原因,对于多索引只有10000次插入,运行程序需要几分钟.我做错了什么或这是预期的吗?

struct A  
{  
  int id;  
  int name;  
  int age;  
  A(int id_,int name_,int age_):id(id_),name(name_),age(age_){}  
};  


/* tags for accessing the corresponding indices*/  
struct id{};  
struct name{};  
struct age{};  

typedef multi_index_container<  
  A,  
  indexed_by<  
    ordered_unique<  
      tag<id>,  BOOST_MULTI_INDEX_MEMBER(A,int,id)>,  
    ordered_non_unique<  
      tag<name>,BOOST_MULTI_INDEX_MEMBER(A,int,name)>,  
    ordered_non_unique<  
      tag<age>, BOOST_MULTI_INDEX_MEMBER(A,int,age)> >  
> A_set;  



int main()  
{  
  A_set es;  

  for (int a = 0; a != 10000; a++) {  
    es.insert(A(a,a+1,a+2));  
  }  
  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

c++ boost

5
推荐指数
1
解决办法
1913
查看次数

了解浮点表示错误; 我的想法有什么问题?

我无法理解为什么有些数字不能用浮点数表示.

我们知道,普通浮点数会有符号位,指数和尾数.例如,为什么不能在这个系统中准确地表示0.1; 我想到的方式是你将10(1010 in bin)放到尾数,-2放到exponent.据我所知,这两个数字都可以在尾数和指数中准确表示.那么为什么我们不能准确地代表0.1?

floating-point floating-accuracy

5
推荐指数
1
解决办法
2140
查看次数

Java和ECMA-262(AS,JS)中正则表达式之间的差异

我需要将Java正则表达式转换为Actionscript正则表达式.

显然没有任何预制转换器,所以我试图自己写一个.有没有列出所有差异的资源?

我知道regular-expressions.info,但似乎并没有全面的差异列表.

谢谢

javascript java regex actionscript-3 ecma262

5
推荐指数
1
解决办法
1873
查看次数

如何在foreach迭代期间更改Set <String>中的字符串?

我有一组字符串,我想迭代,并改变所有相等的东西,等于别的东西:

// Set<String> strings = new HashSet()
for (String str : strings) {
  if (str.equals("foo")) {
    // how do I change str to equal "bar"?
  }
}
Run Code Online (Sandbox Code Playgroud)

我试过replace()哪个不行.我也尝试删除"str"并添加所需的字符串,这会导致错误.我该怎么做呢?

java

3
推荐指数
1
解决办法
6518
查看次数

创建全局FILE*对象?

我正在构建一个程序,它有几个需要从文件中读取数据的函数.由于函数的使用频率相当高,因此打开和关闭每个调用的文件会非常耗时,所以我的计划是将FILE*对象设置为全局,并让文件打开程序的整个持续时间.显然,这是不可能的,因为这:

#include <fstream>
FILE * yhtit;
yhtit = fopen("thefile.txt","r");
int main() {
return 0; }
Run Code Online (Sandbox Code Playgroud)

给出错误: main.cpp|54|error: expected constructor, destructor, or type conversion before ‘=’ token|

保持文件在程序的整个持续时间内打开的最佳方法是什么,而不必单独将FILE*对象传递给需要它的每个函数?

c c++

1
推荐指数
2
解决办法
5399
查看次数

使用正则表达式匹配字符串中的多个 URL

我正在尝试使用此处的正则表达式匹配字符串中的 URL:Regular expression to match URLs in Java

它适用于一个 URL,但是当我在字符串中有两个 URL 时,它只匹配后者。

这是代码:

Pattern pat = Pattern.compile(".*((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])", Pattern.DOTALL);
Matcher matcher = pat.matcher("asdasd http://www.asd.as/asd/123 or http://qwe.qw/qwe");
// now matcher.groupCount() == 2, not 4
Run Code Online (Sandbox Code Playgroud)

编辑:我试过的东西:

// .* removed, now doesn't match anything // Another edit: actually works, see below
Pattern pat = Pattern.compile("((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])", Pattern.DOTALL);

// .* made lazy, still only matches one
Pattern pat = Pattern.compile(".*?((https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|])", Pattern.DOTALL);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

java regex

0
推荐指数
1
解决办法
2436
查看次数