我的目标是在对数刻度上显示 0 值,略低于 1。
我设法with boxes用对数 Y 刻度绘制了自己的简单直方图 ( )。我的 Y 值是非负整数,最大为 25000。我无法区分 0 和 1 值,因为 Y 刻度从 1 开始。这在数学上是正确的,但我想在 1 的正下方添加一个零。
如果我要编写一个绘制图表的程序,我会向所有数据添加 1,并从 Y 标签中删除 1。有什么技巧可以为我做类似的事情吗?
我有一个仅限标题的项目.在里面我有一堂课.在它内部(或其他任何地方)我希望有恒定的数据(枚举值为字符串和反之亦然).这个问题似乎比我预想的要困难得多.
typedef boost::bimap<MyEnum,std::string> Data;
Run Code Online (Sandbox Code Playgroud)
我尝试过但没有用的东西:
static Data const s_data = _initData();:错误就像:only static const integral data members can be initialized within a class.
static Data const * const s_pData = _initData();:该_initData()函数有一个静态局部变量(在第一次调用时填充),并返回它的地址.没有与上述相同的原因工作.
我尝试和工作过,但我觉得它很难看:
class Ugly {
public:
static MyEnum lookupByName(std::string s)
{
MyEnum ret;
lookup(ret,s,true);
return ret;
}
static String lookupByEnum(MyEnum e)
{
std::string ret;
lookup(e,ret,false);
return ret;
}
static void lookup(MyEnum &e, std::string &s, bool etos)
{
static Data s_data = _fill();
if(etos) …Run Code Online (Sandbox Code Playgroud) 该cppreference页说,有关std::basic_string::swap它具有恒定的复杂性.我认为这意味着不能发生复制内容,只能交换指针或类似内容.我写了一个测试代码并且经历过它确实在VS2010下移动了内容.测试代码:
std::string s1("almafa");
std::string s2("kortefa");
std::cout << "s1.c_str(): "<< (void*)s1.c_str() << std::endl;
std::cout << "s2.c_str(): "<< (void*)s2.c_str() << std::endl;
std::cout << "SWAP!" << std::endl;
s1.swap(s2);
std::cout << "s1.c_str(): "<< (void*)s1.c_str() << std::endl;
std::cout << "s2.c_str(): "<< (void*)s2.c_str() << std::endl;
Run Code Online (Sandbox Code Playgroud)
g ++ 4.6.3上的输出
s1.c_str(): 0x22fe028
s2.c_str(): 0x22fe058
SWAP!
s1.c_str(): 0x22fe058
s2.c_str(): 0x22fe028
Run Code Online (Sandbox Code Playgroud)
VS2010的输出
s1.c_str(): 000000000022E2D0
s2.c_str(): 000000000022E320
SWAP!
s1.c_str(): 000000000022E2D0
s2.c_str(): 000000000022E320
Run Code Online (Sandbox Code Playgroud)
它是标准的分歧还是我不知道的事情发生了?
假设我有一些文件和流的内部框架.我有IOutputStream带接口类write(char const *buffer, size_t size)和flush().我有一个工具,称为Printer可以与任何IOutputStream后代实例一起使用.然后我有Printer & operator<<(T x)样式方法,其中T x是要写入的数据(或引用或指向它的指针).
例如,Printer & operator<<(int x)将转换x为字符串,并将引用的输出流的write(...)函数调用为real.
让我们看看问题!调用:printer << "appletree";.它叫Printer & operator<<(char const *s).对于这种用法,我必须调用一个strlen(s)来确定大小,然后我可以调用最后一步.这是相当疯狂的,因为我知道appletree编译时的长度.
这有什么好的做法吗?STL如何ostream与Titerals一起玩?
const一个方法的限定符应该保护数据成员不被错误覆盖.如果你有一个数据成员是一个指针,那么只有指针被保护,指向的值不是.它是C++设计中的一个缺陷还是它有什么基本的东西?
这是一个演示这种情况的代码.在报告不相关的错误和风格问题之前,请考虑其唯一目的是证明上述情况并且简短明了.
#include <cstdio>
#include <cstdlib>
#include <cstring>
class Cat
{
public:
Cat(char const *name)
: _name(strdup(name))
{ }
~Cat(){ free(_name); }
void SetName(char const *name)
{
free(_name);
_name = strdup(name);
}
char const* GetName() const
{
_name[0] = 'P';
return _name;
}
private:
char *_name;
};
int main()
{
Cat c("lost+found");
c.SetName("Molly");
printf("%s\n",c.GetName());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用以下命令编译没有警告和错误:
g++ -W -Wall -Wextra -pedantic -Os pmc.cpp -o pmc
Run Code Online (Sandbox Code Playgroud)
结果程序的输出是Polly.
更新使用迂腐char const *而不是传统const char *
我有一个带指针(pBegin和pEnd)的字符范围.我认为它是一个字符串,但它没有\0终止.如何std::cout有效打印?
std::string我们有好的解决方案吗?如果没有,最顺畅的解决方法是什么?
我正在使用函数(命名get)创建对结构成员的引用,然后使用另一个函数(命名pr)移动结构,然后我取消引用先前创建的指针.
我在这里错了(也就是意外工作),或者我的参考仍然有效吗?
struct MyStruct {
inner: i32,
}
fn get(a: &MyStruct) -> &i32 {
return &a.inner;
}
fn pr(a: MyStruct) {
println!("MyStruct {}", a.inner);
}
fn main() {
println!("Hello, world!");
let x = MyStruct { inner: 3 };
let &i = get(&x);
pr(x);
println!("i {}", i);
}
Run Code Online (Sandbox Code Playgroud)
的锈操场输出:
Hello, world!
MyStruct 3
i 3
Run Code Online (Sandbox Code Playgroud) 架构:
persons (id, name, birthyear, gender)
pets (id, person_id, name, leg_count)
plants (id, person_id, kind, qty)
我想制作一份关于按人分组的这些事情的只读报告.完成人员列表(没有相关记录).我希望每个人都有"子表".就像是:
Persons
+----+------+-----------+--------+
| id | name | birthyear | gender |
+----+------+-----------+--------+
| 1 | Joe | 1980 | M |
+----+------+-----------+--------+
| Pets |
| +----+------+-----------+ |
| | id | name | Leg count | |
| +----+------+-----------+ |
| | 1 | Rex | 4 | |
| +----+------+-----------+ |
| | 2 | Ka | 0 | |
| …Run Code Online (Sandbox Code Playgroud) 我们过度使用模板,我们不能总是告诉手头类型的签名,所以我们需要隐藏最终优化的警告的技术.我有一个简单的ASSERT(condition)宏,如果条件没有评估为真,它会抛出一些东西.
目标是检查T键入count值的范围.我们需要它至少为零,最多为最大值size_t.
template<typename SomeIntegral>
SomeIntegral ZERO()
{
return SomeIntegral(0);
}
template<typename T>
class C
{
public:
void f(T count)
{
std::vector<std::string> ret;
ASSERT(count>=ZERO<T>()); // Check #1
ASSERT(count<std::numeric_limits<size_t>::max()); // Check #2
ret.reserve(size_t(count)); // Needs check #1 and #2 to succeed.
// ...
}
// ...
};
Run Code Online (Sandbox Code Playgroud)
#1检查在没有警告的情况下编译,但#2检查说comparison between signed and unsigned integer expressions,因为在这种特殊情况下,计数具有签名类型.如果我能ASSERT((unsigned T) count < std::numeric_limits<size_t>::max())以某种方式说或类似......在这种情况下转换为无符号T是安全的,因为我们从#1知道它至少为零.
...或者我可以使用其他编译器不可知的方法?
我们希望在两个数字类型之间进行检查数字转换,而不会在失败时触发异常抛出.就像是:
bool numeric_cast(Source s, Target &t)
Run Code Online (Sandbox Code Playgroud)
我们的项目中提升了错误处理,生成了调用堆栈和其他一些昂贵的东西.我有可能转换失败的地方,我不想在每次失败时付出那么高的代价.
我有一个链接错误,我认为我不应该有:
koala.o:
In function `ns1::utils::io::protocol::InputSequenceFile
<
ns1::utils::io::protocol::TargetSequenceProtocol<
ns1::utils::io::FooIndexTarget
>
, false
>::InputSequenceFile(ns1::utils::io::DataFileDescriptor const&) [clone .constprop.1291]':
koala.cpp:(.text+0x332a):
undefined reference to
`ns1::utils::io::DataFileFactory::createIndexedInputFile
(
ns1::utils::io::DataFileDescriptor const&, ns1::utils::io::IndexMode::Enum, bool
)'
Run Code Online (Sandbox Code Playgroud)
(我只更改了空格,以便更容易阅读。我还非常小心地做了一些匿名化。)
然后我曾经nm发现有关符号的情况:
nm other.o:
0000000000008f20 t _ZN4ns15utils2io15DataFileFactory22createIndexedInputFileERKNS1_18DataFileDescriptorENS1_9IndexMode4EnumEb.constprop.1677
nm koala.o:
U _ZN4ns15utils2io15DataFileFactory22createIndexedInputFileERKNS1_18DataFileDescriptorENS1_9IndexMode4EnumEb
Run Code Online (Sandbox Code Playgroud)
(grepped 输出)
它们是相同的,不计算constprop.1677(我不知道它是什么)。所讨论的方法是类的静态方法。我需要一些帮助来了解问题的根本原因。
我需要解释为什么下面的代码不能编译.我有一个解决方法,我将在下面阐述,但我不明白原始版本的失败.
加速代码读取:概念是定义一个接口(ISomething),然后创建一个抽象实现(ASomething),它(2)使用第一个(尚未定义的)实现第二个函数(1).从抽象的(例如SomethingImpl)派生的完整实现必须定义第一个方法,并具有覆盖第二个方法的选项.
#include <iostream>
class ISomething
{
public:
virtual ~ISomething()
{ }
virtual int f(int x) = 0; // (1)
virtual int f(int x, int y) = 0; // (2)
};
class ASomething
: public virtual ISomething
{
public:
virtual int f(int x, int y) // (2)
{
return f(x) + f(y); // (3)
}
};
class SomethingImpl
: public ASomething
{
public:
virtual int f(int x) // (1)
{ …Run Code Online (Sandbox Code Playgroud)