我对链表有疑问.我有以下结构和功能,例如.
struct node {
int value;
struct node *next;
};
struct entrynode {
struct node *first;
struct node *last;
int length;
};
void addnode(struct entrynode *entry) {
struct node *nextnode = (struct node *)malloc(sizeof(struct node));
int temp;
if(entry->first == NULL) {
printf("Please enter an integer.\n");
scanf("%d", &temp);
nextnode->value = temp;
nextnode->next = NULL;
entry->first = nextnode;
entry->last = nextnode;
entry->length++;
} else {
entry->last->next = nextnode;
printf("Please enter an integer.\n");
scanf("%d", nextnode->value);
nextnode->next = NULL;
entry->last = nextnode;
entry->length++;
} …Run Code Online (Sandbox Code Playgroud) 假设我有一堆水果:
class Fruit { ... };
class Apple : public Fruit { ... };
class Orange: public Fruit { ... };
Run Code Online (Sandbox Code Playgroud)
以及在所述水果上运行的一些多态函数:
void Eat(Fruit* f, Pesticide* p) { ... }
void Eat(Apple* f, Pesticide* p) { ingest(f,p); }
void Eat(Orange* f, Pesticide* p) { peel(f,p); ingest(f,p); }
Run Code Online (Sandbox Code Playgroud)
好,等一下.停在那儿.请注意,任何理智的人都会使Eat()成为Fruit类的虚拟成员函数.但这不是一种选择,因为我不是一个理智的人.另外,我不想在我的水果类的头文件中使用Pesticide*.
遗憾的是,我希望接下来要做的就是成员函数和动态绑定允许的内容:
typedef list<Fruit*> Fruits;
Fruits fs;
...
for(Fruits::iterator i=fs.begin(), e=fs.end(); i!=e; ++i)
Eat(*i);
Run Code Online (Sandbox Code Playgroud)
显然,这里的问题是我们传递给Eat()的指针将是Fruit*,而不是Apple*或Orange*,因此什么都不会被吃掉,我们都会非常饥饿.
所以我真的希望能够做到而不是这样:
Eat(*i);
Run Code Online (Sandbox Code Playgroud)
这是:
Eat(MAGIC_CAST_TO_MOST_DERIVED_CLASS(*i));
Run Code Online (Sandbox Code Playgroud)
但是根据我有限的知识,这种魔法并不存在,除非可能是一个充满调用dynamic_cast的大讨厌if语句.
那么有一些我不知道的运行时魔法吗?或者我应该实现并维护一个充满dynamic_casts的令人讨厌的if语句?或者我应该把它搞砸,不要再考虑如何在Ruby中实现它,并允许一点Pesticide进入我的水果标题?
更新:而不是使用裸饮食功能和农药的人为设计,而是假设我只是不想把吃水果放在水果中,因为它毫无意义.知道怎么吃的水果?算了吧.相反,我需要一个带有Eat功能的Eater类,使用不同的代码来吃每种水果,以及一些默认代码,以防它是食者无法识别的水果:
class Eater
{
public:
void Eat(Apple* f) { wash(); nom(); …Run Code Online (Sandbox Code Playgroud) 我在Eclipse中使用App Engine设置JUnit时遇到了麻烦.我正确地设置了JUnit,也就是说,我可以运行不涉及数据存储区或其他服务的测试.但是,当我尝试在我的测试中使用数据存储区时,它们会失败.我正在尝试的代码来自App Engine站点(见下文):
http://code.google.com/appengine/docs/java/tools/localunittesting.html#Running_Tests
到目前为止,我已经添加了外部JAR(使用Eclipse)appengine-testing.jar.但是当我运行测试时,我得到了下面的例外.所以,我显然不理解从上面提到的网页启用服务的说明.有人可以清理在Eclipse中提供App Engine服务所需的步骤吗?
java.lang.NoClassDefFoundError: com/google/appengine/api/datastore/dev/LocalDatastoreService
at com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig.tearDown(LocalDatastoreServiceTestConfig.java:138)
at com.google.appengine.tools.development.testing.LocalServiceTestHelper.tearDown(LocalServiceTestHelper.java:254)
at com.cooperconrad.server.MemberTest.tearDown(MemberTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: com.google.appengine.api.datastore.dev.LocalDatastoreService
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 25 more
Run Code Online (Sandbox Code Playgroud)
这是实际的代码(几乎从网站上复制): …
当我尝试在IntelliJ中创建一个简单的模块时,它响应:
Fatal Error: Unable to find package java.lang in classpath or bootclasspath
Run Code Online (Sandbox Code Playgroud) 我们有一个具有高性能需求的消息处理系统.最近我们注意到第一条消息比后续消息长了许多倍.当这通过我们的系统时,发生了一堆转换和消息扩充,其中大部分是通过外部库完成的.
我只是简要介绍了这个问题(使用callgrind),将一条消息的"运行"与许多消息的"运行"进行比较(提供比较基线).
我看到的主要区别是函数"do_lookup_x"占用了大量的时间.查看对此函数的各种调用,它们似乎都由公共函数调用:_dl_runtime_resolve.不知道这个函数做了什么,但对我来说这看起来像是第一次使用各种共享库,然后由ld加载到内存中.
这是正确的假设吗?二进制文件在准备好使用之前不会将共享库加载到内存中,因此我们会看到第一条消息大幅减速,但后续没有?
我们如何避免这种情况?
注意:我们以微秒级操作.
我正在使用gzip压缩字符串,然后解压缩结果,但是我得到了以下异常,为什么?
output: Exception in thread "main" java.util.zip.ZipException: oversubscribed dynamic bit lengths tree at java.util.zip.InflaterInputStream.read(Unknown Source) at java.util.zip.GZIPInputStream.read(Unknown Source) at Test.main(Test.java:25)
public class Test {
public static void main(String[]args) throws IOException{
String s="helloworldthisisatestandtestonlydsafsdafdsfdsafadsfdsfsdfdsfdsfdsfdsfsadfdsfdasfassdfdfdsfdsdssdfdsfdsfd";
byte[]bs=s.getBytes();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(outstream);
gzipOut.write(bs);
gzipOut.finish();
String out=outstream.toString();
System.out.println(out);
System.out.println(out.length());
ByteArrayInputStream in = new ByteArrayInputStream(out.getBytes());
GZIPInputStream gzipIn=new GZIPInputStream(in);
byte[]uncompressed = new byte[100000];
int len=10, offset=0, totalLen=0;
while((len = gzipIn.read(uncompressed, offset, len)) >0){ // this line
offset+=len;
totalLen+=len;
}
String uncompressedString=new String(uncompressed,0,totalLen); …Run Code Online (Sandbox Code Playgroud) 我不是在谈论应用程序分析器或调试器,而是更具体地管理生产环境中的应用程序.因此,实质上是监控,识别瓶颈,部署修复程序.
如果我有类似的东西:
object value = null;
Foo foo = new Foo();
PropertyInfo property = Foo.GetProperties().Single(p => p.Name == "IntProperty");
property.SetValue(foo, value, null);
Run Code Online (Sandbox Code Playgroud)
然后foo.IntProperty设置为0,即使value = null.看起来它正在做类似的事情IntProperty = default(typeof(int)).我想抛出一个InvalidCastExceptionif IntProperty不是"可以为空"的类型(Nullable<>或引用).我正在使用Reflection,所以我不提前知道类型.我该怎么做呢?
这是一个用例:
我有一个桌面应用程序(使用Eclipse RCP构建),在启动时弹出一个对话框,其中包含"UserName"和"Password"字段.一旦最终用户输入他的UserName和Password,就会联系一个服务器(一个spring remote-servlet,客户端是一个spring httpclient:类似于这里的方法 .),并且在服务器端执行身份验证.
与上述情况有关的几个问题:
请让我知道您的设计/建筑评论/建议.感谢您的帮助.
c# ×2
c++ ×2
eclipse ×2
java ×2
asp.net ×1
c ×1
classpath ×1
default ×1
gzip ×1
inheritance ×1
junit ×1
ld ×1
linux ×1
numbers ×1
performance ×1
polymorphism ×1
random ×1
reflection ×1
security ×1
visitor ×1