我正在使用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 <'不匹配
我怀疑我没有对运算符进行重载,因为它应该做,但我无法确切地指出确切的问题.我在这做错了什么?
我有以下代码(主要遵循这里的第一个示例: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) 我无法理解为什么有些数字不能用浮点数表示.
我们知道,普通浮点数会有符号位,指数和尾数.例如,为什么不能在这个系统中准确地表示0.1; 我想到的方式是你将10(1010 in bin)放到尾数,-2放到exponent.据我所知,这两个数字都可以在尾数和指数中准确表示.那么为什么我们不能准确地代表0.1?
我需要将Java正则表达式转换为Actionscript正则表达式.
显然没有任何预制转换器,所以我试图自己写一个.有没有列出所有差异的资源?
我知道regular-expressions.info,但似乎并没有全面的差异列表.
谢谢
我有一组字符串,我想迭代,并改变所有相等的东西,等于别的东西:
// 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"并添加所需的字符串,这会导致错误.我该怎么做呢?
我正在构建一个程序,它有几个需要从文件中读取数据的函数.由于函数的使用频率相当高,因此打开和关闭每个调用的文件会非常耗时,所以我的计划是将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*对象传递给需要它的每个函数?
我正在尝试使用此处的正则表达式匹配字符串中的 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)
有任何想法吗?