时间:2019-05-16 标签:c++vectorlast element field

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14275291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:10:56  来源:igfitidea点击:

c++ vector last element field

c++vectorstructure

提问by berndh

I have a vector vecof structures. Such a structure has elements int a, int b, int c. I would like to assign to some int varthe element c, from the last structure in a vector. Please can you provide me with this simple solution? I'm going circle in line like this:

我有一个向量vec结构。这样的结构具有元素int a, int b, int c。我想从向量中的最后一个结构中将元素 c分配给某个int var。请你能给我提供这个简单的解决方案吗?我要像这样绕圈:

var = vec.end().c;

回答by WhozCraig

The immediate answer to your question as to fetching access to the last element in a vector can be accomplished using the back()member. Such as:

关于获取对向量中最后一个元素的访问权的问题的直接答案可以使用该back()成员来完成。如:

int var = vec.back().c;

Note: If there is a possibility your vector is empty, such a call to back()causes undefined behavior. In such cases you can check your vector's empty-state priorto using back()by using the empty()member:

注意:如果您的向量可能为空,则此类调用会back()导致未定义的行为。在这种情况下,您可以使用之前back()使用empty()成员检查向量的空状态:

if (!vec.empty())
   var = vec.back().c;

Likely one of these two methods will be applicable for your needs.

这两种方法中的一种可能适用于您的需求。

回答by CygnusX1

vec.end()is an iterator which refers the after-the-end location in the vector. As such, you cannot deference it and access the member values. vec.end()iterator is always valid, even in an empty vector (in which case vec.end() == vec.begin())

vec.end()是一个迭代器,它引用向量中的结尾位置。因此,您不能顺从它并访问成员值。vec.end()迭代器始终有效,即使在空向量中(在这种情况下vec.end() == vec.begin()

If you want to access the last element of your vector use vec.back(), which returns a reference (and not iterator). Do note however that if the vector is empty, this will lead to an undefined behavior; most likely a crash.

如果要访问向量的最后一个元素,请使用vec.back(),它返回一个引用(而不是迭代器)。但是请注意,如果向量为空,这将导致未定义的行为;很可能是崩溃。

回答by Veger

Use back():

使用back()

var = vec.back().c;

回答by Chowlett

var = vec.back().c;is what you want.

var = vec.back().c;是你想要的。

end()returns the iterator (not an element) past-the-end of the vector. back()returns a reference to the last element. It has a counterpart front()as well.

end()返回向量末尾的迭代器(不是元素)。back()返回对最后一个元素的引用。它也有一个对应物front()

回答by Ivaylo Strandjev

Try this: var = vec.back().c;

尝试这个: var = vec.back().c;

Also you may modify your code like:

您也可以修改您的代码,如:

var = vec.rbegin()->c;

In both versions first make sure that the vector is not empty!

在这两个版本中,首先确保向量不为空!

回答by mkaes

You can simply use backas it returns a reference to the last element.
var = vec.back().c

您可以简单地使用,back因为它返回对最后一个元素的引用。
var = vec.back().c

回答by Lars

You can use the std:vector<T>:back()function, e.g. vec.back().c. See http://www.cplusplus.com/reference/vector/vector/back/

您可以使用该std:vector<T>:back()功能,例如vec.back().c。见http://www.cplusplus.com/reference/vector/vector/back/