我有一个函数,该函数获取一个样本(an std::vector<double>)作为输入并计算该样本的平均值:处理空输入向量情况的最佳方法是什么?
我的第一个想法是在此代码段中引发一个异常:
double average(const std::vector<double>& sample)
{
size_t sz = sample.size();
if (sz==0) throw std::exception("unexpected empty vector");
double acc = 0;
for (size_t i=0; i<sz; ++i) acc += sample[i];
return acc/sz;
}
Run Code Online (Sandbox Code Playgroud)
但我认为另一种解决方案可能是返回NaN:
double average(const std::vector<double>& sample)
{
size_t sz = sample.size();
if (sz==0) return std::numeric_limits<double>::quiet_NaN();
double acc = 0;
for (size_t i=0; i<sz; ++i) acc += sample[i];
return acc/sz;
}
Run Code Online (Sandbox Code Playgroud)
我喜欢异常,因为它显示了问题发生的位置,而如果我在长时间计算的最终结果中得到NaN,则我将很难理解NaN的出生地。无论如何,对于NaN,我都喜欢返回“特殊”双精度值以表示发生了意外情况的可能性。
还有其他方法可以解决空向量吗?谢谢。
我正在尝试使用XML包将GPS运行数据从TCX文件导入到R中.这是我的数据的一小部分样本(只有3个跟踪点,而不是~900)
<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Running">
<Id>2011-10-30T16:05:48Z</Id>
<Lap StartTime="2011-10-30T16:05:48Z">
<TotalTimeSeconds>3855.99</TotalTimeSeconds>
<DistanceMeters>12498.8115</DistanceMeters>
<MaximumSpeed>4.45662498</MaximumSpeed>
<Calories>1011</Calories>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2011-10-30T16:05:48Z</Time>
<Position>
<LatitudeDegrees>52.33613318</LatitudeDegrees>
<LongitudeDegrees>-1.58814317</LongitudeDegrees>
</Position>
<AltitudeMeters>77.5234375</AltitudeMeters>
<DistanceMeters>0.00000000</DistanceMeters>
</Trackpoint>
<Trackpoint>
<Time>2011-10-30T16:05:49Z</Time>
<Position>
<LatitudeDegrees>52.33614810</LatitudeDegrees>
<LongitudeDegrees>-1.58814283</LongitudeDegrees>
</Position>
<AltitudeMeters>77.5234375</AltitudeMeters>
<DistanceMeters>1.77584004</DistanceMeters>
</Trackpoint>
<Trackpoint>
<Time>2011-10-30T16:05:54Z</Time>
<Position>
<LatitudeDegrees>52.33627098</LatitudeDegrees>
<LongitudeDegrees>-1.58818323</LongitudeDegrees>
</Position>
<AltitudeMeters>76.0814209</AltitudeMeters>
<DistanceMeters>15.7694969</DistanceMeters>
</Trackpoint>
</Track>
</Lap>
</Activity>
</Activities>
</TrainingCenterDatabase>
Run Code Online (Sandbox Code Playgroud)
而我正在尝试使用跟踪点读取
doc = xmlParse("filetest.tcx")
xmlToDataFrame(nodes = getNodeSet(doc, "//Trackpoint"))
Run Code Online (Sandbox Code Playgroud)
但是这会失败,结果是一个空的数据框.但是我发现如果我删除
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"
Run Code Online (Sandbox Code Playgroud)
从文件开头的TrainingCenterDatabase标记开始,此导入按预期正确运行.IE浏览器.使用以下数据:
<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase>
<Activities>
<Activity Sport="Running">
<Id>2011-10-30T16:05:48Z</Id>
<Lap StartTime="2011-10-30T16:05:48Z">
<TotalTimeSeconds>3855.99</TotalTimeSeconds>
<DistanceMeters>12498.8115</DistanceMeters>
<MaximumSpeed>4.45662498</MaximumSpeed>
<Calories>1011</Calories> …Run Code Online (Sandbox Code Playgroud) 给定a std::vector< std::string >,矢量按字符串长度排序,如何找到等长强度的范围?
我期待着C++中的惯用解决方案.
我找到了这个解决方案:
// any idea for a better name? (English is not my mother tongue)
bool less_length( const std::string& lhs, const std::string& rhs )
{
return lhs.length() < rhs.length();
}
std::vector< std::string > words;
words.push_back("ape");
words.push_back("cat");
words.push_back("dog");
words.push_back("camel");
size_t length = 3;
// this will give a range from "ape" to "dog" (included):
std::equal_range( words.begin(), words.end(), std::string( length, 'a' ), less_length );
Run Code Online (Sandbox Code Playgroud)
有没有一种标准的方法(漂亮)?
从std :: map的最后n个元素创建std :: vector的C++惯用方法是什么?
我对保存向量中的顺序不感兴趣.
我可以复制元素,如下所示:
std::map< double, MyType > m;
size_t n = 3;
std::vector< MyType > v;
std::map< double, MyType >::iterator it = m.end();
while ( n-- ) { // assuming m.size() >= n
it--;
v.push_back(it->second);
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有其他方式,更惯用,做到这一点?
我正在使用OpenCV和C++.我想检查图像是否是另一个图像的一部分,并且已经找到了一个matchTemplate正在工作的函数.但是如果模板图像有点不同呢?有没有一个函数或类似方法matchTemplate检查模板是否是源图像的一部分,但是具有公差参数,如位置,角度,大小甚至可能变形?或者我需要一个完全不同的方法而不是模板匹配?
这是我到目前为止的代码,它在源图像中找到模板图像,但没有(或几乎没有)容差.
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
/// Global Variables
Mat img; Mat templ; Mat result;
const char* image_window = "Source Image";
const char* result_window = "Result window";
int match_method;
int max_Trackbar = 5;
/// Function Headers
void MatchingMethod( int, void* );
/**
* @function main
*/
int main( int, char** argv )
{
/// …Run Code Online (Sandbox Code Playgroud) c++ opencv image-processing computer-vision template-matching
在C++中:
const size_t N = 1000;
int* p = new int[N];// time=t0
Run Code Online (Sandbox Code Playgroud)
我的程序只有一个线程,在分配内存后p,我的程序只会读取指向的内存p.
标准对价值的评价是p什么?
将p保持,直到它得到在时间价值= T0 delete的p?
或者,操作系统可以自行决定重新分配指向的内存p吗?
它取决于价值N吗?
我需要区分两个向量的任意两个元素.如果A<-c(1,2)和B<-c(3,4)那么我的结果R应该是c(3-1,3-2,4-1,4-2).
有了这个片段
myfunction <- function(N)
{
A = runif(N)
B = runif(N)
R = c()
for(a in A){
for(b in B){
R=c(b-a,R)
}
}
R
}
print(system.time(result <- myfunction(300)))
Run Code Online (Sandbox Code Playgroud)
我得到这个时间
user system elapsed
14.27 0.01 14.39
Run Code Online (Sandbox Code Playgroud)
有没有更快的方法呢?
我有一段计算两个平方之和的代码float:
float a, b, c;
// assign some random float to b and c
a = b*b+c*c;
Run Code Online (Sandbox Code Playgroud)
a两个 s 的平方和可以float为负数吗?
原始代码片段位于函数内部,因此提出问题的另一种方式如下:
bool fun(float b, float c)
{
return b*b+c*c<0;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何一对值b和c给出fun(b,c)==true?
我想在int和 C++ 平凡类型之间编写一个双射,例如doubleor float。
双射在编译时是已知的。
我想像这样使用它(大写表示宏):
INIT(42,float)
INIT(-17,double)
#include <type_traits>
int main() {
const int a = TYPE_TO_INT(float);
static_assert(a==42);
INT_TO_TYPE(-17) pi;
static_assert(std::is_same<double,decltype(pi)>::value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)