假设我有std::vector发言权Vector
现在在对向量(插入或删除)执行某些操作之后,我想检查向量是否为空,并且基于此我想要执行某些操作.
哪种方法更好
方法1
if (Vector.size() == 0){ /* operations */ }
Run Code Online (Sandbox Code Playgroud)
方法2
if (Vector.empty()) { /* operations */ }
Run Code Online (Sandbox Code Playgroud)
哪种方法更好,1还是2?
嗨我想通过标量值来(乘法,加法等)矢量,例如myv1 * 3,我知道我可以用forloop做一个函数,但有没有办法用STL函数做到这一点?像{Algorithm.h :: transform function}这样的东西?
从向量中获取char数组的最简单方法是什么?
我正在做的方法是使用vector begin和end迭代器初始化一个字符串,然后从这个字符串中获取.c_str().还有其他有效的方法吗?
我试过了norm,但我认为它给出了错误的结果.(规范c(1, 2, 3)是sqrt(1*1+2*2+3*3),但它返回6..
x1 <- 1:3
norm(x1)
# Error in norm(x1) : 'A' must be a numeric matrix
norm(as.matrix(x1))
# [1] 6
as.matrix(x1)
# [,1]
# [1,] 1
# [2,] 2
# [3,] 3
norm(as.matrix(x1))
# [1] 6
Run Code Online (Sandbox Code Playgroud)
有谁知道在R中计算向量范数的函数是什么?
Vector是同步的,ArrayList不是同步的,但是我们可以同步一个ArrayList Collections.synchronizedList(aList),这样会更好更快地执行吗?
我想避免不必要的副本.我的目标是:
std::ifstream testFile( "testfile", "rb" );
std::vector<char> fileContents;
int fileSize = getFileSize( testFile );
fileContents.reserve( fileSize );
testFile.read( &fileContents[0], fileSize );
Run Code Online (Sandbox Code Playgroud)
(这不起作用,因为reserve实际上没有在向量中插入任何东西,所以我无法访问[0]).
当然,std::vector<char> fileContents(fileSize)有效,但是初始化所有元素的开销(fileSize可能相当大).同样的resize().
这个问题与开销的重要程度无关.相反,我只是想知道是否有另一种方式.
我正在使用openFrameworks编写应用程序,但我的问题不仅仅是针对oF; 相反,它是关于C++向量的一般性问题.
我想创建一个包含另一个类的多个实例的类,但也提供了一个直观的界面来与这些对象进行交互.在内部,我的类使用了类的向量,但是当我尝试使用vector.at()操作对象时,程序将编译但不能正常工作(在我的情况下,它不会显示视频).
// instantiate object dynamically, do something, then append to vector
vector<ofVideoPlayer> videos;
ofVideoPlayer *video = new ofVideoPlayer;
video->loadMovie(filename);
videos.push_back(*video);
// access object in vector and do something; compiles but does not work properly
// without going into specific openFrameworks details, the problem was that the video would
// not draw to screen
videos.at(0)->draw();
Run Code Online (Sandbox Code Playgroud)
在某个地方,有人建议我制作一个指向该类对象的指针向量,而不是这些对象本身的向量.我实现了这一点,它确实像一个魅力.
vector<ofVideoPlayer*> videos;
ofVideoPlayer * video = new ofVideoPlayer;
video->loadMovie(filename);
videos.push_back(video);
// now dereference pointer to object and call draw
videos.at(0)->draw();
Run Code Online (Sandbox Code Playgroud)
我是动态地为对象分配内存,即 ofVideoPlayer = …
我读过其他类似的帖子,但我不明白我做错了什么.我认为我对载体的声明是正确的.我甚至试图宣布没有大小,但即使这样也行不通.出了什么问题?我的代码是:
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
using namespace std;
vector<string> v2(5, "null");
vector< vector<string> > v2d2(20,v2);
class Attribute //attribute and entropy calculation
{
vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);
public:
Attribute(){}
int total,T,F;
};
int main()
{
Attribute attributes;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有关于创建矢量的问题.如果我这样做a <- 1:10,"a"的值为1,2,3,4,5,6,7,8,9,10.
我的问题是如何在元素之间创建具有特定间隔的向量.例如,我想创建一个值为1到100但仅以5为间隔计数的向量,以便得到一个值为5,10,15,20,...,95,100的向量
我认为在Matlab中我们可以做到1:5:100,我们如何使用R?
我可以尝试做5*(1:20)但有更短的方法吗?(因为在这种情况下我需要知道整个长度(100),然后除以间隔(5)的大小得到20)
我知道通常标准对已经移动的值的要求很少:
N3485 17.6.5.15 [lib.types.movedfrom]/1:
可以从(12.8)移动C++标准库中定义的类型的对象.可以显式指定或隐式生成移动操作.除非另有规定,否则此类移动物体应置于有效但未指定的状态.
我找不到任何与vector此明确排除的内容.但是,我无法想出一个理智的实现,导致向量不为空.
是否有一些标准需要我缺少或者类似于在C++ 03中作为连续缓冲区处理basic_string?