说我有一个长长的清单A值(比如长度为1000),为此我要计算std成对的100,也就是我想计算std(A(1:100)),std(A(2:101)),std(A(3:102)),..., std(A(901:1000)).
在Excel/VBA中,可以通过=STDEV(A1:A100)在一个单元格中写入然后一次填充来轻松实现此目的.现在我的问题是,如何在Matlab中有效地实现这一点,而无需使用任何昂贵的for循环.
编辑:是否也可以为时间序列列表执行此操作,例如,当A的尺寸为1000 x 4(即4个时间序列长度为1000)时?输出矩阵的尺寸应为901 x 4.
假设我们有这个代码片段
#include <vector>
int main()
{
std::vector<int> a = { 1,2 };
a.push_back(3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在 VS 2019 中,我尝试单步执行 (F11) constructor 和push_back函数,但 VS 只是跳过它。
还有其他解决方案,例如在 Visual Studio 2017 中调试 C++ 应用程序步骤不是我的代码,有没有办法关闭此功能?或者在 Visual Studio 2012 中调试 C++ 代码时跳过 STL 代码?,实际上要求相反(将步骤转为关闭)。所以我尝试扭转他们的解决方案,例如添加
<Function><Name>std::.*</Name><Action>StepInto</Action></Function>,
C:\...\Visual Studio\2019\Professional\Common7\Packages\Debugger\Visualizers\default.natstepfilter
但它不起作用。
我正在使用Debug x64这些选项
运行/JMC /permissive- /GS /W4 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc142.pdb" /Zc:inline /fp:precise /D "_CRT_SECURE_NO_WARNINGS" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++17 /FC …
我目前正在使用此代码通过 Python 生成电子邮件:
from email import generator
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def Create_Email():
msg = MIMEMultipart('alternative')
msg['Subject'] = 'My Subject'
msg['To'] = 'test@gmail.com'
html = """\
<html>
<head></head>
<body>hello world</body>
</html>"""
part = MIMEText(html, 'html')
msg.attach(part)
outfile_name = r'C:\Downloads\email_sample.eml'
with open(outfile_name, 'w') as outfile:
gen = generator.Generator(outfile)
gen.flatten(msg)
Create_Email()
Run Code Online (Sandbox Code Playgroud)
但当我用 Outlook 打开该文件时,它显示为一封已发送的电子邮件:
如何更改此设置,以便将保存的文件视为草稿,我仍然可以编辑然后发送?就像这样:
如果email.generator不能做到这一点,我很乐意使用替代包。
我正在尝试重载类的成员函数(类似于C++中可以完成的操作)。所以我读到,在Rust中,人们必须使用特征来实现这一目标。下面是一些示例代码(注意,这只是为了演示这个想法):
/* my_class.rs */
pub struct MyClass {
pub a: i32,
pub b: i32,
}
pub trait Func<T> {
fn func(&self, t: T) -> Self;
}
impl Func<i32> for MyClass {
fn func(&self, t: i32) -> MyClass {
MyClass { a: self.a + t, b: self.b }
}
}
impl Func<&str> for MyClass {
fn func(&self, t: &str) -> MyClass {
MyClass { a: self.a, b: self.b + t.parse::<i32>().unwrap() }
}
} …Run Code Online (Sandbox Code Playgroud) 查看的实现std::is_same我们可以看到一些内部函数(继承自integral_constant)。为方便起见,让我复制 g++ 代码:
template<typename _Tp, _Tp __v>
struct integral_constant {
static constexpr _Tp value = __v;
typedef _Tp value_type;
typedef integral_constant<_Tp, __v> type;
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; }
};
template<typename, typename>
struct is_same : public integral_constant<bool, false> { };
template<typename _Tp>
struct is_same<_Tp, _Tp> : public integral_constant<bool, true> { };
Run Code Online (Sandbox Code Playgroud)
这为我们提供了如何使用它的几个选项:
bool a1 = is_same<int, int>{}; // instantiate & then cast implicitly
bool …Run Code Online (Sandbox Code Playgroud) 假设我在 Excel 中的某个单元格上有一个很长的公式
=IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 01", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 02", IF(SUM(D3:D6)>1,"KLABCDEFQTUJHI) -VWXYZ 03", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 04", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 05", IF(SUM>(D3:D6)) 1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 06", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 07", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ)" :D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 09", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 10", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 11", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ:12DUM)" >1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 13", IF(SUM(D3:D6)>1,"ABCDEFGHIJKLMNOPQRSTU-VWXYZ 14","no" ))))))))))))))ABCDEFGHIJKLMNOPQRSTU-VWXYZ 14","no" )))))))))))))))ABCDEFGHIJKLMNOPQRSTU-VWXYZ 14","no" )))))))))))))))
我运行以下 VBA 代码
Private Sub ExecuteFormula()
Dim sFormula As String, vReturn As Variant
sFormula = Selection.Formula
vReturn = Application.Evaluate(sFormula)
If VarType(vReturn) <> vbError Then
MsgBox vReturn, vbInformation
Else
MsgBox "Error", vbExclamation
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
然后我得到“错误”。对于较短的公式,它工作得很好,所以我想知道是否有办法使用 VBA 评估长公式(一般)。
The below class is meant to be a top-layer class which brings all the benefits of nlohman::json but offers additional functions.
#include <nlohmann/json.hpp>
class Other { /* ... */ };
class AbstractData : public nlohmann::json
{
public:
AbstractData (const nlohmann::json& json) : nlohmann::json(json) { }
Other createOther(const char* key) { /* create Other class using key */ }
std::string toString() { /* convert to string */ }
/* etc. */
};
Run Code Online (Sandbox Code Playgroud)
But I ran into issues when using operator[]. …
我有以下示例代码
template<class T1, class T2>
class Operation
{
public:
constexpr Operation(const T1& lhs, const T2& rhs) noexcept
: m_lhs(lhs), m_rhs(rhs) { }
private:
const T1& m_lhs;
const T2& m_rhs;
};
int main()
{
constexpr int a = 3;
constexpr int b = 4;
constexpr Operation op(a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用cygwin(gcc 8.2)编译
template<class T1, class T2>
class Operation
{
public:
constexpr Operation(const T1& lhs, const T2& rhs) noexcept
: m_lhs(lhs), m_rhs(rhs) { }
private:
const T1& m_lhs;
const T2& m_rhs; …Run Code Online (Sandbox Code Playgroud) 在下面的示例中,我们使用C ++ 17功能“类模板参数推导”来推断val类型为Base<int, double, bool>:
template<class T, class U, class V>
struct Base {
Base(T, U) { };
Base(T, U, V) { };
Base(V) { };
};
void func() {
Base val(1, 4., false);
}
Run Code Online (Sandbox Code Playgroud)
现在,是否可以部分指定模板参数,然后推导出其余参数?实际上是这样的:
Base<V = bool> val1(1, 4.); // U & V deduced --> Base<int, double, bool>
Base<T = bool, T = int> val2(5.); // V deduced --> Base<bool, int, double>
Run Code Online (Sandbox Code Playgroud)
我试过了
template<class T, class U> using Base2 = Base<T, U, …Run Code Online (Sandbox Code Playgroud) 在以下代码示例中,Visual Studio 给出错误“堆已损坏”。起初,for 循环似乎工作正常,但经过 1 或 2 次迭代后,它就崩溃了。
我觉得我的函数myReAllocate促进了某种不应该发生的内存泄漏(因为当我将其注释掉时,一切正常)。究竟发生了什么?这似乎是一个非常微妙的错误。
#include <iostream>
using namespace std;
class myClass{};
void myReAllocate(myClass* c)
{
delete c;
c = new myClass();
}
int main(void)
{
myClass *cc[10];
for (int i = 0; i < 10; i++){
cout << i << " attempt" << endl;
cc[i] = new myClass();
myReAllocate(cc[i]);
delete cc[i];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过添加一个operator=,但也没有帮助。
myClass operator=(myClass& right) {
myClass temp = right;
return *this;
}
Run Code Online (Sandbox Code Playgroud) c++ operator-overloading heap-memory new-operator delete-operator
C ++ 17是否提供任何在编译时加载/读取文件的方式?更具体地说,我想将文件的内容加载到const char*或std::string_view等等。例如:
constexpr const char* read_file_content(const char* file_path) {
/* should read in the content of the file and return it */
return "";
}
constexpr const char* file_path = "some/folder/file.json";
constexpr const char* file_content = read_file_content(file_path);
Run Code Online (Sandbox Code Playgroud)
我遇到过这个答案是否可以在编译时读取文件?从2014年开始。teivaz的解决方案使用一些宏和#include魔术完美地工作了。但是,它需要更改输入文件:该文件应将其内容包含在STR(...)中,但这可能并不总是可能的。
由于上述答案很旧,我认为可能情况已经改变,也许C ++ 17(或可能的C ++ 20)提供了一些功能来克服此问题。
c++ ×7
c++17 ×5
constexpr ×3
class ×1
debugging ×1
email ×1
excel ×1
file-io ×1
function ×1
heap-memory ×1
inheritance ×1
matlab ×1
new-operator ×1
overloading ×1
python ×1
python-3.x ×1
reference ×1
rust ×1
stl ×1
template-argument-deduction ×1
templates ×1
traits ×1
vba ×1
vector ×1