e**********n 发帖数: 359 | 1 #include "temp.hh"
#include
main() {
StringSet constStrs;
for(StringSet::iterator it = constStrs.begin(); it!=constStrs.end();
it++) {
std::cout << *it <<" ";
}
}
其中 “temp.hh" 为:
#include
#include
class StringSet : public std::set {
public:
StringSet(){
insert( std::string("0"));
insert( std::string("1"));
......
insert( std::string("3999"));
};
};
总共往StringSet里添加了4000个字符串,此处为了简单用0到3999,但实际上可以是任
意的字符串。
用 g++ -g 编译很快完事 |
|
m*****l 发帖数: 95 | 2 随便给个思路。
public static Set generateStrings(String input) {
Set stringSet = new HashSet();
if(input != null) {
List questionMarks = new ArrayList();
char[] chars = input.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '?') {
questionMarks.add(i);
} else if (chars[i] != '0' && chars[i] != '1') {
throw new Illega... 阅读全帖 |
|
d**a 发帖数: 84 | 3 这样写没啥问题, 你为啥要继承呢?
====================================================
using namespace std;
typedef set StringSet;
inline string to_string(int i) {
stringstream ss;
ss<
return ss.str();
}
int main() {
StringSet constStrs;
for(int i=0; i<4000; ++i) {
constStrs.insert(to_string(i));
}
for(StringSet::iterator it = constStrs.begin(); it!=constStrs.end();
it++) {
std::cout << *it <<" ";
}
}
); |
|
d**a 发帖数: 84 | 4 下面在我这里也没问题
Ubuntu 2.6.28-15-generic, x86_64
g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
============================
#include "temp.hh"
#include
main() {
StringSet constStrs;
for(StringSet::iterator it = constStrs.begin(); it!=constStrs.end();
it++) {
std::cout << *it <<" ";
}
}
===============temp.hh===============
#include
#include
#include
using std::string;
using std::stringstream;
inline string to_string(int i) {
str |
|
e**********n 发帖数: 359 | 5 I moved the strings to a global array
char *constStr[] = {"0", "1"...."3999"};
and inserted the strings into a StringSet using a for loop. This time g++ -O3
finished quickly, and the size of
the executable file is also 10 times smaller.
I don't think inheriting from STL container is the cause here. g++ seems to
do too much optimization for the
4000 lines of insert(...), also probably in an incorrect way. The for loop
gives g++ little to optimize. |
|
p*****2 发帖数: 21240 | 6 Copy and Paste
A combination of interfaces and good ol’ copy and paste is the way Go
currently implements sortable slices. This is of course a terrible, terrible
idea. The names of your types will end up being StringSet, IntSet, and
FloatSet. When you find a bug you’ll be forced to go through all of the
instances where you copied and pasted, fix it and hope you don’t miss any.
This is clearly not a sustainable way to ‘implement’ generics. |
|