English 中文(简体)
利用果园作为病媒指数
原标题:Using a char as a vector index
  • 时间:2012-01-13 00:24:48
  •  标签:
  • c++

Say,我有“条形码”和“条码”的载体。 根据护卫的特点,我要确定相应的病媒指数。

std::vector<bool> is_present(256, false);
for (int i = 0; i < str.size(); ++i)
{
    is_present[str[i]] = true;
}

根据我的理解,该标准没有界定一个果园的签字性质。 视平台而定,可以签署或签署。 在大多数平台上,经签署的果园将有两个8倍的补数(-128至127),未签署的果园将是8倍的未签名的ger(0至255)。

If that is the case, is there a possibility that str[i] will return a negative number and cause a memory fault in is_present[str[i]]? or is the char getting typecast to vector<bool>::size_type which is unsigned and hence no problems can occur?

而且,最好使用<条码>查询和设计;bool> is_present(pow(2, CHAR_BIT)),不实的,而不是将其硬化为256?

最佳回答

如果你想对数值作出界定,则浏览途径将<条码><>char/code>至<条码>。

您可以说:1u << CHAR_BIT,以达到预期的规模。

std::vector<bool> is_present(1u << CHAR_BIT, false);
for (int i = 0; i < str.size(); ++i)
{
    is_present[static_cast<unsigned char>(str[i])] = true;
}
问题回答

如果你真的希望这样做,那么,我就使用<条码>,即:数字-有限与设计;std:string:char_type>:min(,然后减去从价值到确保它永远从0开始的价值,果园类型的范围:

#include <string>
#include <vector>
#include <limits>

int main() {
  const std::string str = "hello world";
  std::vector<bool> is_present(256, false);
  for (std::string::size_type i = 0; i < str.size(); ++i) {
    is_present[static_cast<int>(str[i]) - std::numeric_limits<std::string::value_type>::min()] = true;
  }
}

您也希望使用<代码>max()和min(>,用于病媒的最初规模。

重新分类的类型首先将价值扩大至适当规模,然后可能改变类型。 在<代码><cctype>功能中,人们可使用“植被”的偏差之处之一是,这些功能只能用积极的价值观加以使用,<<>char>/code>可能产生消极价值。 例如,在一个支持ISO-拉丁美洲-1(ISO/IEC 8859-1:1998)的地方,试图将我的名字上层,这将在签署<条码>的平台上产生灾难性结果。

The proper way to deal with this to use e.g. static_cast<unsigned char>(c) or, assuming you have included <cinttypes>, something like std::uint8_t(c). To determine the appropriate size of an array of char you would, of course, use std::numeric_limits<unsigned char>::max(): using pow() is a bit of an overkill for this. In general, you would rather use the shift operators (on an unsigned type) than pow() when you need powers of 2.

如果是这种情况,则有可能使遗体恢复到负数。

并造成记忆失误?

是的,你们会从这一法典中找到不明确的行为。

或者说,果园的类型为vector<bool>:size_type。 页: 1

是的,它将被转换成非签字类型。

因而不会出现问题?

No, this will wrap and give you a "very large" number instead, and it is the access to this value outside your array bounds that will cause the problem

因此,你必须首先明确加入<条码>。

is_present[static_cast<unsigned char>(str[i])] = true

我认为,保证以正确的方式完成这项工作。

此外,使用媒介是否更好,不是把它硬化到256?

在我个人方面,我对char不是8条轨道的系统感到担忧,我怀疑,其他许多事情可能首先发生。 如果我真的担心的话,我很可能这样做。

#if CHAR_BIT!=8
   #error "This code will not work for non-8bit-char systems"
#endif

static_cast<unsigned char>(c) is OK. Converting char to unsigned char acts like filling bits of unsigned char with bits of source char.

我更喜欢<代码>CHAR_ FairUCHAR_ Fair/code>至pow,simple and separate.

我的问题是,为什么扼杀的果园会是负面的,而它却会被用作辅助工具? 我指的是,如果它是病媒或某些病媒类似的集装箱的子体,它必须是积极的。 我认为,你真的希望是肯定的。

类似法律:

std::vector<bool> is_present(CHAR_MAX, false);
for (int i = 0; i < str.size(); ++i)
{
    assert( str[i]>=0 && str[i]<=127 );
    is_present[str[i]] = true;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?