我和同事讨论过简单字符串解析器的实现.一个是"小",10行代码,使用c ++和流,另一个是70行代码,使用switch case并通过char迭代字符串char.我们测试了超过100万次迭代,并使用时间命令测量速度.似乎漫长而丑陋的方法平均快1秒.
问题:输入:字符串
"v=spf1 mx include:_spf-a.microsoft.com include:_spf-b.microsoft.com include:_spf-c.microsoft.com include:_spf-ssg-a.microsoft.com ip4:131.107.115.212 ip4:131.107.115.215 ip4:131.107.115.214 ip4:205.248.106.64 ip4:205.248.106.30 ip4:205.248.106.32 ~all a:1.2.3.4"
输出:
map<string, list<string>>包含每个键的所有值,例如:ip4,include,a
上面给出的输入字符串上的一次迭代的示例输出:
关键:一
1.2.3.4,
关键:包括
_spf-a.microsoft.com,_spf-b.microsoft.com,_spf-c.microsoft.com,_spf-ssg-a.microsoft.com,
关键:IP4
131.107.115.212,131.107.115.215,131.107.115.214,205.248.106.64,205.248.106.30,205.248.106.32,
"小而美"的解析器:
istringstream iss(input);
map<string, list<string> > data;
string item;
string key;
string value;
size_t pos;
while (iss.good()) {
iss >> item;
pos = item.find(":");
key = item.substr(0,pos);
data[key].push_back(item.substr(pos+1));
}
Run Code Online (Sandbox Code Playgroud)
第二种更快的方法:
typedef enum {I,Include,IP,A,Other} State;
State state = Other;
string line = input;
string value;
map<string, list<string> > data;
bool end = …Run Code Online (Sandbox Code Playgroud) 给定(在C++中)
char * byte_sequence;
size_t byte_sequence_length;
char * buffer;
size_t N;
Run Code Online (Sandbox Code Playgroud)
假设byte_sequence并byte_sequence_length初始化为某个任意长度的字节序列(及其长度),并buffer初始化为指向N * byte_sequence_length字节,那么复制byte_sequence到buffer N时间的最简单方法是什么?STL/BOOST中有什么东西可以做这样的事吗?
例如,如果序列是"abcd",并且N是3,那么buffer最终将包含"abcdabcdabcd".
我最近接到了一个C++访谈,我被问到,编译器如何区分两个不同类中具有相同名称的静态数据成员?
由于所有静态数据变量都存储在数据段中,因此编译器必须通过这种方式跟踪哪些静态数据属于哪个类,尤其是当它们具有相同名称时.
编辑:我回答了名字错误,但他拒绝说名字错误仅在同一类的成员中使用.
我正在为一个学校项目开发一个C++的小型虚拟机,它应该像dc命令一样工作,由一个输出输出元素,一个芯片组,一个Cpu和Ram组成.我目前正在研究芯片组,我已经实现了一个小的解析类,以便能够从标准输入或文件中获取一些Asm指令,然后将此指令推送到Cpu.
问题是:我的指令在std :: list中排序,我希望能够通过foreach指令逐个推送它们.为此,我需要能够将我的成员函数"push_instruction"称为for_each的函数指针F; 我无法找到这样做的伎俩......
有任何想法吗?这是我的代码:
/*
** Function which will supervise
** the lexing and parsing of the input (whether it's std_input or a file descriptor)
** the memory pushing of operands and operators
** and will command the execution of instructions to the Cpu
*/
void Chipset::startExecution()
{
/*
** My parsing
** Instructions
**
*/
for_each(this->_instructList.begin(), this->_instructList.end(), this->pushInstruction);
}
void Chipset::pushInstruction(instructionList* instruction)
{
if (instruction->opCode == PUSH)
this->_processor->pushOperand(instruction->value, Memory::OPERAND, Memory::BACK);
else if (instruction->opCode == ASSERT)
this->_processor->pushOperand(instruction->value, Memory::ASSERT, …Run Code Online (Sandbox Code Playgroud) 我需要一个可以从Scala和Java调用的构建器库.使用默认的命名参数在Scala中足够简单.但是如何从Java调用此代码?见下文.或者我应该使用更流行的两种语言更常见的API?
斯卡拉:
case class Person(gender:Gender.Value, firstName:String, lastName:String){
def fullName = lastName+", "+firstName
override def toString = firstName+","+lastName+","+gender
}
case class PersonBob(
gender:Gender = GenderBob().build,
firstName:String = null,
lastName:String = null) {
def build = Person(
gender,
if(firstName == null) NameBob(if(gender==Gender.Male) engMaleNames
else engFemaleNames).build else firstName,
if(lastName==null) NameBob(engLastNames).build
else lastName
)
}
Run Code Online (Sandbox Code Playgroud)
用法:
val p1 = PersonBob().build
val p2 = PersonBob(lastName = "Jones").build
Run Code Online (Sandbox Code Playgroud)
Java用法:
Person p1 = ?
Person p2 = ?
Run Code Online (Sandbox Code Playgroud) 如何在MATLAB中将strel转换为double?我尝试了几个演员没有成功.我找不到任何有用的东西.谢谢你的帮助.
当我使用 Google Test 和 Google Mock 时,将诊断级别设置为“信息”,我收到如下消息:
Uninteresting mock function call - taking default action specified at:
src/pkgtest/test_Foo.cpp:216:
Function call: GetBar()
Returns: 4-byte object <00-00 00-00>
Stack trace:
Uninteresting mock function call - taking default action specified at:
src/pkgtest/test_Foo.cpp:126:
Function call: GetBaz()
Returns: {}
Stack trace:
unknown file: Failure
C++ exception with description "Uninteresting mock function call - returning default value.
Function call: CreateGrille(@0x7fff6a557050 { 16-byte object <80-D8 BB-01 00-00 00-00 70-D8 BB-01 00-00 00-00> })
The mock function has …Run Code Online (Sandbox Code Playgroud) 考虑以下(简化)代码:
enum eTestMode
{
TM_BASIC = 1, // 1 << 0
TM_ADV_1 = 1 << 1,
TM_ADV_2 = 1 << 2
};
...
int m_iTestMode; // a "bit field"
bool isSet( eTestMode tsm )
{
return ( (m_iTestMode & tsm) == tsm );
}
void setTestMode( eTestMode tsm )
{
m_iTestMode |= tsm;
}
Run Code Online (Sandbox Code Playgroud)
这是可靠,安全和/或良好的做法吗?或者除了使用const int而不是enum之外,还有更好的方法来实现我想做的事情吗?我更喜欢枚举,但代码可靠性比可读性更重要.
我正在尝试将boost :: signal的触发包装到boost :: bind对象中.所以我想要的是在调用boost :: function时用一些预先打包的参数调用信号.
我有的是这个:
boost::signals2::signal<void(int)> sig;
boost::function<void()> f = boost::bind(
&(sig.operator()), &sig, 10);
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我收到以下错误:错误:没有匹配函数调用bind(,...
我也试过这个:
boost::function<void()> f = boost::bind(
(void(boost::signals2::signal<void(int)>::*)(int))
&(sig.operator()), &sig, 10);
Run Code Online (Sandbox Code Playgroud)
但后来我得到了"没有上下文类型信息的重载函数的地址".
那么什么是正确的语法?
我已经阅读了C++面试问题的答案,其中有一个令我困惑的问题:
问:C++编译器何时创建临时变量?
答:如果函数参数是"const引用",则编译器按以下两种方式生成临时变量.
a)实际参数是正确的类型,但它不是Lvalue
Run Code Online (Sandbox Code Playgroud)double Cube(const double & num) { num = num * num * num; return num; } double temp = 2.0; double value = cube(3.0 + temp); // argument is a expression and not a Lvalueb)实际参数的类型错误,但是可以转换为正确类型的类型
Run Code Online (Sandbox Code Playgroud)long temp = 3L; double value = cuberoot(temp); // long to double conversion
我的问题是,一旦函数参数是一个const引用,为什么编译器生成临时变量,是不是自相矛盾?此外,如果函数Cube无法编译,因为它修改了const参数?
c++ ×8
boost ×2
boost-bind ×1
c ×1
casting ×1
coding-style ×1
const ×1
enums ×1
foreach ×1
googlemock ×1
googletest ×1
java ×1
matlab ×1
parsing ×1
reference ×1
reliability ×1
scala ×1
stl ×1
temporary ×1