我有一个像这样的数字条目的数据框
test <- data.frame(x = c(26, 21, 20), y = c(34, 29, 28))
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得以下矢量?
> 26, 34, 21, 29, 20, 28
Run Code Online (Sandbox Code Playgroud)
我能够使用以下内容获得它,但我想应该有一个更优雅的方式
X <- test[1, ]
for (i in 2:dim(test)[ 1 ]){
X <- cbind(X, test[i, ])
}
Run Code Online (Sandbox Code Playgroud) 如果我将迭代器指向向量的最后一个元素时,它会增加2怎么办?在这个询问如何通过2个元素调整到STL容器的迭代器的问题中,提供了两种不同的方法:
当迭代器指向STL容器的最后一个元素或更远时,我用VC++ 7测试了它们的边缘情况:
vector<int> vec;
vec.push_back( 1 );
vec.push_back( 2 );
vector<int>::iterator it = vec.begin();
advance( it, 2 );
bool isAtEnd = it == vec.end(); // true
it++; // or advance( it, 1 ); - doesn't matter
isAtEnd = it == vec.end(); //false
it = vec.begin();
advance( it, 3 );
isAtEnd = it == vec.end(); // false
Run Code Online (Sandbox Code Playgroud)
我已经看过有时可以建议在遍历vector和其他容器时与vector :: end()进行比较:
for( vector<int>::iterator it = vec.begin(); it != vec.end(); …Run Code Online (Sandbox Code Playgroud) 我有一个std :: vector m_vPaths; 我会迭代这个向量并调用:: DeleteFile(strPath).如果我成功删除了该文件,我将从矢量中删除它.我的问题是,我可以使用两个向量吗?是否有不同的数据结构可能更适合我需要做的事情?
示例:使用迭代器几乎可以实现我想要的,但问题是一旦使用迭代器擦除,所有迭代器都将变为无效.
std::vector<std::string> iter = m_vPaths.begin();
for( ; iter != m_vPaths.end(); iter++) {
std::string strPath = *iter;
if(::DeleteFile(strPath.c_str())) {
m_vPaths.erase(iter);
//Now my interators are invalid because I used erase,
//but I want to continue deleteing the files remaining in my vector.
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用两个向量,我将不再有问题,但是有没有更好,更有效的方法来做我想做的事情?
顺便说一句,如果不清楚,m_vPaths就是这样声明的(在我的课上):
std::vector<std::string> m_vPaths;
Run Code Online (Sandbox Code Playgroud) 最近我一直在问编写一个函数读取二进制文件到std::vector<BYTE>哪里BYTE是unsigned char.我很快就找到了这样的东西:
#include <fstream>
#include <vector>
typedef unsigned char BYTE;
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<BYTE> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
Run Code Online (Sandbox Code Playgroud)
这似乎是不必要的复杂,并且char*我在呼叫时被迫使用的明确演员file.read并没有让我感觉更好.
另一种选择是使用std::istreambuf_iterator:
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
// read the data:
return std::vector<BYTE>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()); …Run Code Online (Sandbox Code Playgroud) 以下代码:
#include <vector>
struct S
{
int x, y;
};
int main()
{
std::vector<S> v;
v.emplace_back(0, 0);
}
Run Code Online (Sandbox Code Playgroud)
使用GCC编译时出现以下错误:
In file included from c++/4.7.0/i686-pc-linux-gnu/bits/c++allocator.h:34:0,
from c++/4.7.0/bits/allocator.h:48,
from c++/4.7.0/vector:62,
from test.cpp:1:
c++/4.7.0/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = S; _Args = {int, int}; _Tp = S]':
c++/4.7.0/bits/alloc_traits.h:265:4: required from 'static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = S; _Args = {int, int}; _Alloc = std::allocator<S>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]'
c++/4.7.0/bits/alloc_traits.h:402:4: required …Run Code Online (Sandbox Code Playgroud) 有没有一种很好的方法来区分python中的行和列向量?到目前为止,我正在使用numpy和scipy,我到目前为止看到的是,如果我要给一个向量,说
from numpy import *
Vector = array([1,2,3])
Run Code Online (Sandbox Code Playgroud)
他们不能说天气我的意思是行或列向量.此外:
array([1,2,3]) == array([1,2,3]).transpose()
True
Run Code Online (Sandbox Code Playgroud)
在"现实世界"中哪个是不真实的.我意识到来自上述模块的载体上的大多数功能都不需要区分.例如outer(a,b)或者a.dot(b)我想为了自己的方便而区分.
dplyr::select 导致data.frame,如果结果是一列,有没有办法让它返回一个向量?
目前,我必须执行额外的step(res <- res$y)将其转换为data.frame中的vector,请参阅此示例:
#dummy data
df <- data.frame(x = 1:10, y = LETTERS[1:10], stringsAsFactors = FALSE)
#dplyr filter and select results in data.frame
res <- df %>% filter(x > 5) %>% select(y)
class(res)
#[1] "data.frame"
#desired result is a character vector
res <- res$y
class(res)
#[1] "character"
Run Code Online (Sandbox Code Playgroud)
如下:
res <- df %>% filter(x > 5) %>% select(y) %>% as.character
res
# This gives strange output
[1] "c(\"F\", \"G\", \"H\", \"I\", \"J\")"
# I need:
# [1] …Run Code Online (Sandbox Code Playgroud) 我试图std::vector通过使用以下代码删除所有内容
vector.erase( vector.begin(), vector.end() );
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
更新:不清除破坏向量所持有的元素吗?我不想那样,因为我还在使用这些对象,我只是想清空容器
我遇到了与VectorDrawables的新向后兼容性问题.在支持库中,23.2是一个新功能,用于向后兼容所生成的Android VectorDrawables.
我有一个ImageView,它是一个SelectorDrawable分配给.这个Drawable拥有几个VectorDrawables所以我认为我应该使用app:srcCompat来兼容.但它在我的Galaxy S2上使用android 4.1.2不起作用.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_gps_fixed_24dp"android:state_activated="true" android:state_selected="true"></item>
<item android:drawable="@drawable/ic_gps_not_fixed_24dp" android:state_activated="true" android:state_selected="false"></item>
<item android:drawable="@drawable/ic_gps_not_fixed_24dp" android:state_activated="false" android:state_selected="true"></item>
<item android:drawable="@drawable/ic_gps_off_24dp" android:state_activated="false" android:state_selected="false"></item>
<item android:drawable="@drawable/ic_gps_not_fixed_24dp"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
所有drawables都是vector xml文件.
当使用此SelectorDrawable与srcCompat时,我收到此错误:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_gps_fixed_24dp.xml from drawable resource ID #0x7f0201c1
at android.content.res.Resources.loadDrawable(Resources.java:1951)
at android.content.res.Resources.getDrawable(Resources.java:672)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881).xml from drawable resource ID #0x7f0201c1
Run Code Online (Sandbox Code Playgroud)
使用android:src更糟糕.
如果我使用app:srcCompat中的一个矢量drawable,一切正常.所以我猜这是SelectorDrawable和兼容性的问题.
有没有人有同样的问题,并找到了解决方案,或者目前无法在Android 5之前的SelectorDrawables中使用VectorDrawables?
简要说明:
android vector android-appcompat android-support-library android-vectordrawable
有人能说出使用数组向量的正确方法是什么?
我声明了一个数组(vector<float[4]>)的向量,但error: conversion from 'int' to non-scalar type 'float [4]' requested在尝试时得到resize了.出了什么问题?