我正在尝试使用ruby hpricot库从在线htmls中提取一些数据.我使用firefox扩展fire bug来获取所选项目的xpath.
生成的xpath表达式中始终存在额外的tbody标记.在某些情况下,我必须从表达式中删除tbody标记以获取结果,而在其他情况下,我必须保留标记以获得结果.
我只是无法弄清楚何时保留tbody标签以及什么时候不要.
#include <string>
#include <iostream>
template <typename T>
T max(T a, T b) {
return a > b ? a : b;
}
class Dummy {
private:
std::string name;
int age;
public:
Dummy(int an_age) {age = an_age;}
bool operator> (Dummy &a) {return age > a.age;}
std::string toString() const {return "The age is " + age;}
};
std::ostream& operator<<(std::ostream& out, const Dummy& d) {return out<< d.toString();}
int main()
{
std::cout << max(3, 7) << std::endl;
std::cout << max(3.0, 7.0) << std::endl;
std::cout …Run Code Online (Sandbox Code Playgroud) 我知道为数组分配了一个连续的内存块.
我的第一个问题是当数组元素是一个对象而不是内置类型时,什么存储在为数组保留的连续内存中?对象的指针或对象的实际数据?我的猜测是指针存储在数组中,实际的对象随机存储在堆中.我对么?
我的第二个问题是现在我们想要为一个对象数组保留一个指定的内存(例如,共享内存).实现这一目标的最佳方法是什么?我应该逐个序列化指定内存中的实际对象并使用相对指针(例如索引)来访问它们中的每一个吗?
int buf1[] = {0,0,0,0,0};
int* buf2 = new int[5]; //assume every element is initialzed to 0 as well
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一区别是buf1是对数组的引用,而buf2是指向数组的指针.换句话说,buf1总是引用数组,而buf2也指向其他地方.
除了上面提到的那个,声明(和初始化数组)的两种方式之间还有什么区别吗?
<HTML>
<BODY>
<%
// This scriptlet declares and initializes "date"
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! The time is now
<%
out.println( date );
out.println( "<BR>Your machine's address is " );
out.println( request.getRemoteHost());
%>
</BODY>
</HTML>
Run Code Online (Sandbox Code Playgroud)
在上面的JSP代码中,我以为request.getRemoteHost()会返回浏览器主机的ip地址.然而,它返回的内容如下:0:0:0:0:0:0:0:1.有什么问题?
import xlrd
book = xlrd.open_workbook("univ_list.xls")
sh = book.sheet_by_index(0)
for r in range(sh.nrows)[1:]: # line 4
print sh.row(r)[:4] # line 5
Run Code Online (Sandbox Code Playgroud)
[1:]第4行的含义是什么?[:4]第5行的意思是什么?
<a href="http://www.utoronto.ca/gdrs/" title="Rehabilitation Science"> Rehabilitation Science</a>
Run Code Online (Sandbox Code Playgroud)
对于上面的例子,我想同时获取部门名称“康复科学”及其主页网址“http://www.utoronto.ca/gdrs/”。
有人可以建议一些可以为我完成这项工作的智能正则表达式吗?
#include <stdio.h>
#include <string.h>
int main() {
char* p = new char[10];
memset(p,0,10);
printf("%c",*p);
}
Run Code Online (Sandbox Code Playgroud)
我想memset设置开始每个字节p来0.看到什么都没打印出来,我有点惊讶.究竟发生了memset什么?
MemoryManager openMemory() {
if (...) {
return memory_manager_instance;
}
else
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
MemoryManager是用户定义的C++类的名称.上面的函数定义给出了标题中的错误.
基本上我不想在条件不成立时返回实例.这样的函数定义是有效的,我通常用Java做的,但它似乎不适用于C++.我该怎么做才能解决这个问题?
System.out.println("neon.mems.cmu.edu/people/".split("/").length); // output is 2
Run Code Online (Sandbox Code Playgroud)
我正在做一些网址处理.令我惊讶的是,我刚刚得到了上面的结果.我认为元素的数量可以是分割器的数量加一.
null到目前为止,我没有意识到最后一个空字符串(或只是)从分割数组中被切断.我想知道每种编程语言是否都是这种情况.
import java.util.regex.*;
public class Splitter {
public static void main(String[] args) throws Exception {
Pattern p = Pattern.compile("[,\\s]+");
String[] results = p.split("one,two, three four , five");
for (String result : results) {
System.out.println(result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
拆分器可以是逗号或空格,也可以是任意数量的组合.我认为它的正则表达式应该是[,\s]+.为什么示例中会有额外的反斜杠?