c ++方式? [英] The c++ way?

查看:70
本文介绍了c ++方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据结构,其中包含长度为n的缓冲区。


这种情况​​下的缓冲区包含一个非空终止的字符串。


目前获得一个空终止缓冲区我正在做以下事情:


strncpy(buf,(const char *)data,n);

buf [n] =''\''';

字符串x(buf);


肯定必须有一个更好的方法来初始化字符串x ...


顺便说一下,在C ++中有像Variant这样的东西在

Visual Basic中?

I have a data structure that contains a buffer of character of lenght n.

The buffer in this case contains a string that is not null terminated.

at the moment to get a null terminated buffer I''m doing the following:

strncpy(buf, (const char *)data, n);
buf[n] = ''\0'';
string x(buf);

Surely there must be a better way to initialze the string x...

By the way is there such a thing as a Variant in C++ as there is in
Visual Basic?

推荐答案

* JustSomeGuy:
* JustSomeGuy:
我的数据结构包含长度为n的缓冲区。

这种情况下的缓冲区包含一个非空终止的字符串。

目前获得一个空终止缓冲区我正在做以下事情:

strncpy(buf,(const char *)data,n);
buf [n] =''\ 0 ';
字符串x(buf);

肯定有一个更好的方法来初始化字符串x ...



关闭袖口,


字符串x(数据,数据+ n);


顺便说一句就是Variant这样的东西在C ++中,因为在Visual Basic中有?
I have a data structure that contains a buffer of character of lenght n.

The buffer in this case contains a string that is not null terminated.

at the moment to get a null terminated buffer I''m doing the following:

strncpy(buf, (const char *)data, n);
buf[n] = ''\0'';
string x(buf);

Surely there must be a better way to initialze the string x...

Off the cuff,

string x( data, data+n );

By the way is there such a thing as a Variant in C++ as there is in
Visual Basic?




不在语言或其标准库中,但有类似
$ b $的内容b它在Boost库中。


然而,如果你很聪明,你会注册VB部分的思维模式

你的大脑 - 在C ++中采用这种做法并不是一个好主意。


-

答:因为它弄乱了人们的顺序通常阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是最多的在usenet和电子邮件中烦人的事情?



Not in the language or its standard library, but there is something like
it in the Boost library.

However, if you''re smart you''ll register for a mindwipe for the VB parts
of your brain -- it''s not a good idea to adopt such practices in C++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


JustSomeGuy发布:
JustSomeGuy posted:
顺便提一下有这样的事情吗?瓦里安在C ++中用于Visual Basic?
By the way is there such a thing as a Variant in C++ as there is in
Visual Basic?



以下是VB中Variant类型的工作原理:

class Variant

{

私人:


enum

{

intE,
charE,

doubleE

} current_type;


union

{

int intD;

char charD;

double doubleD;

}数据;

public:

//分配运营商和其他运营商一起来这里

};


As你可以看到,Variant'的速度很慢,浪费了更多的内存。这是一个双输的b $ b b情形,但如果你是其中之一,那么Visual Basic,Dumb it

Down人们,你可能会大放异彩!

-JKop


Here is how the Variant type in VB works:
class Variant
{
private:

enum
{
intE,
charE,
doubleE
} current_type;

union
{
int intD;
char charD;
double doubleD;
} data;

public:
//Assignment operator goes here, as well as other operators
};

As you can see, Variant''s are slow, and waste more memory. It''s a lose-lose
situation, but then again if you''re one of those "Visual Basic, Dumb it
Down" people, you may take a shine to it!
-JKop


JustSomeGuy写道:
JustSomeGuy wrote:
我有一个数据结构,其中包含长度为n的缓冲区。

在这种情况下,缓冲区包含一个非空终止的字符串。

此刻得到一个空终止缓冲区我正在做以下事情:

strncpy(buf,(const char *)数据,n);
buf [n] =''\''' ;
字符串x(buf);

肯定有一个更好的方法来初始化字符串x ...


是的。您将上述内容放入数据结构的方法中,并在需要字符串时始终调用

。然后,如果字符串表示

发生变化,你只有一个地方可以升级。


但是可能有一种方法可以使用std :: copy或类似的。其他人会回复关于

那个。

顺便说一句,C ++中的Variant就像在Visual Basic中那样?
I have a data structure that contains a buffer of character of lenght n.

The buffer in this case contains a string that is not null terminated.

at the moment to get a null terminated buffer I''m doing the following:

strncpy(buf, (const char *)data, n);
buf[n] = ''\0'';
string x(buf);

Surely there must be a better way to initialze the string x...
Yes. You put the above into a method of your data structure, and always call
that function when you need the string. Then, if the string representation
ever changes, you only have one place to upgrade.

But there might be a way using std::copy or similar. Others will reply about
that.
By the way is there such a thing as a Variant in C++ as there is in
Visual Basic?




是的。 MS重命名了VB的插件层COM,并将Variant发布为

低级别类型VARIANT。在C ++中,高级类型_variant_t包含它。


对于便携式解决方案,请转到 www.boost.org 并查看boost :: any。


-

Phlip
< a rel =nofollowhref =http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces\"target =_ blank> http://industrialxp.org/community/bi...UserInterfaces



Yes. MS renamed VB''s plugin layer "COM", and published the Variant as the
low-level type VARIANT. In C++ the high level type _variant_t wraps that.

For a portable solution, go to www.boost.org and look at boost::any.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


这篇关于c ++方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆