我应该使用什么类型的迭代器的区别,以消除和QUOT;数据&QUOT的可能损失;警告?损失、区别、类型、迭代

2023-09-08 01:00:16 作者:回忆酿成的毒

我需要在64位模式下的警告的共同规则。哪种方法更好呢?

考虑的一些code以下行

  const int的N =的std :: max_element(co​​nt.begin(),cont.end()) -  cont.begin();
 

  const int的ARR_SIZE = 1024;
烧焦常用3 [ARR_SIZE]
// ...
const int的N =的std :: max_element(ARR,编曲+ ARR_SIZE) - 编曲;
 

这是我一贯code。我没有问题,86。

但是,如果我运行编译器的64位模式下,我有一些警告:

 从转换'的std :: _ Array_iterator< _Ty,_size> :: difference_type'到'廉政',可能丢失数据
从__int64'到'廉政',可能丢失数据的转换
 
C list的简介及使用

我要解决这些问题,共同统治。哪种方法更好呢?

制作的static_cast

  const int的N =的static_cast< INT>(
     的std :: max_element(co​​nt.begin(),cont.end()) -  cont.begin());
 

我觉得这不是通用的。而太多的字母。

替换输出类型与 ptrdiff_t型

 常量ptrdiff_t型N =的std :: max_element(co​​nt.begin(),cont.end()) -  cont.begin();
 

我应该怎么做,然后用这种未知类型ptrdiff_t型? 我会得到另一个十几警告即可。 我想使许多操作与N:保存,加法,乘法,周期等。 重要:但如果的std :: _ Array_iterator< _Ty,_size> :: difference_type ptrdiff_t型是不同的类型?

替换输出类型与的std :: _ Array_iterator< _Ty,_size> :: difference_type

  // h文件
的struct {
    mymember类型; //是什么类型?
};


// CPP文件
类型定义的std ::矢量< INT> cont_t;
常量cont_t :: difference_type N =的std :: max_element(co​​nt.begin(),cont.end()) -  cont.begin();
//保存ñ
小号MYSTRUCT;
mystruct.mymember = N; //什么类型mystruct.mymember的?
 

我应该如何保存N +什么类型的mystruct.mymember的?我不知道它在h文件。

您的解决方案。

解决方案

如果标准:: _ Array_iterator< _Ty,_size> :: difference_type和ptrdiff_t型不同类型的不要使用这样的编译器。此外,有机会,它没有正式不同。例如。这是一个向量使用默认的标准分配的情况下,因为这就是它获取它的类型定义,但由于正式的担保并不重要(他自己,这真的没有按'T)我不会看这件事中的C ++ 0x草案。

所以,使用 ptrdiff_t型

但它可以是一个好主意,添加一些类型定义,就像

 的typedef ptrdiff_t型尺寸;
的typedef ptrdiff_t型指数;
 

,然后在你的具体情况下,你会使用首页

这些类型定义自然伴随着自独立 countOf STARTOF endOf 功能,使您能够把原始的数组和标准库容器完全相同的方式。

当你看到它的名字首页这是一个有点更清楚,这是一个指标,它不能很自然地走出指数或尺寸 set of类型几乎不管你做什么。例如,添加的东西给它,它仍然是一个首页。所以大多不会有一个另有​​十余警告。

但在某些极少数情况下,你需要从首页去刚 INT ,说。在和这些罕见的情况下,只是做了的static_cast 闭嘴编译器,让你的意图明确。甚至是自定义的的static_cast narrowTo 运行,为前pressiveness ...

干杯和放大器;心连心,

I need a common rule for warnings in x64 mode. Which way is better?

Consider the following lines of some code

const int N = std::max_element(cont.begin(), cont.end()) - cont.begin();

or

const int ARR_SIZE = 1024;
char arr[ARR_SIZE];
//...
const int N = std::max_element(arr, arr + ARR_SIZE) - arr;

It is my usual code. I have no problems with x86.

But if I run compiler in x64 mode I have some warnings:

conversion from 'std::_Array_iterator<_Ty,_Size>::difference_type' to 'int', possible loss of data
conversion from '__int64' to 'int', possible loss of data

I want to solve these problems by common rule. Which way is better?

Making static_cast:

const int N = static_cast<int>(
     std::max_element(cont.begin(), cont.end()) - cont.begin()  );

I think this is not general-purpose. And too much letters.

Replace output type with ptrdiff_t:

const ptrdiff_t N = std::max_element(cont.begin(), cont.end()) - cont.begin();

What should I do then with this unknown type ptrdiff_t? I'll get another dozen warnings then. I want to make many operations with N: save, addition, multiplication, cycles and etc. Important: but what if std::_Array_iterator<_Ty,_Size>::difference_type and ptrdiff_t are different types?

Replace output type with std::_Array_iterator<_Ty,_Size>::difference_type:

//h-file
struct S {
    type mymember; // What is the type?
};


//cpp-file
typedef std::vector<int> cont_t;
const cont_t::difference_type N = std::max_element(cont.begin(), cont.end()) - cont.begin();
// Save N
S mystruct;
mystruct.mymember = N; // What type of mystruct.mymember?

How should I save N? What type of mystruct.mymember? I don't know it in h-file.

Your solution.

解决方案

"what if std::_Array_iterator<_Ty,_Size>::difference_type and ptrdiff_t are different types?" Don't use such a compiler. Also, chances are that it can't formally be different. E.g. this is the case for a vector using the default standard allocator, since that's where it fetches its typedefs, but since the formal guarantee doesn't matter (he he, it really doesn't) I'm not going to look this up in the C++0x draft.

So, use ptrdiff_t.

But it can be a good idea to add a few typedefs, like

typedef ptrdiff_t Size;
typedef ptrdiff_t Index;

and then in your concrete case you'd use Index.

These typedefs are naturally accompanied by custom freestanding countOf, startOf and endOf functions, enabling you to treat raw arrays and standard library containers in exactly the same way.

When you see the name Index it's a bit more clear that it's an index, which can't very naturally get out of the Index or Size set of types almost no matter what you do. E.g., add something to it, it's still an Index. So mostly there will not be a "another dozen warnings".

But in some rare case you'll need to get from Index to just int, say. In and in those rare cases just do a static_cast to shut up the compiler and make your intent clear. Or even a custom static_cast-like narrowTo operation, for expressiveness...

Cheers & hth.,