我读过的大多数编程书都有以下几行:
"X语言不支持真正的多维数组,但你可以用数组数组模拟(近似)它们."
由于我的大部分经验都是基于C语言,即C++,Java,JavaScript,php等,我不确定"真正的"多维数组是什么.
真正的多维数组的定义是什么以及支持它的语言是什么?另外,如果可能的话,请在代码中显示一个真正的多维数组的示例.
language-agnostic language-theory jagged-arrays multidimensional-array
我正在使用a SynchronisedQueue
来进行线程之间的通信.我发现当附加线程在条件变量上等待时销毁线程对象会导致程序崩溃.这可以通过detach()
在线程销毁之前调用来纠正.但我想知道等待条件变量的线程终止后会发生什么.有没有其他方法可以使用条件变量来避免这种情况?
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
template <typename Type> class SynchronisedQueue {
public:
void Enqueue(Type const & data) {
std::unique_lock<std::mutex> lock(mutex_);
queue_.push(data);
condition_.notify_one();
}
Type Dequeue() {
std::unique_lock<std::mutex> lock(mutex_);
while (queue_.empty())
condition_.wait(lock);
Type result = queue_.front();
queue_.pop();
return result;
}
private:
std::queue<Type> queue_;
std::mutex mutex_;
std::condition_variable condition_;
};
class Worker {
public:
Worker(SynchronisedQueue<int> * queue) : queue_(queue) {}
void operator()() {
queue_->Dequeue(); // <-- The thread waits here.
}
private:
SynchronisedQueue<int> * queue_; …
Run Code Online (Sandbox Code Playgroud) 我正在关注React Tutorial,并坚持如何使用React.findDOMNode
.
这是我的代码:
export class CommentForm extends React.Component<{}, {}> {
handleSubmit(e: React.FormEvent) {
e.preventDefault();
console.log(React.findDOMNode(this.refs['author']));
}
render() {
return <form className="commentForm" onSubmit={ e => this.handleSubmit(e) }>
<input type="text" placeholder="Your name" ref="author" />
<input type="text" placeholder="Say something..." ref="text" />
<input type="submit" value="Post" />
</form>;
}
}
Run Code Online (Sandbox Code Playgroud)
呼叫console.log(React.findDOMNode(this.refs['author']));
让我回到<input type="text" data-reactid=".0.2.0" placeholder="Your name">
控制台.但是,我无法弄清楚如何检索输入元素的值(我输入框中输入的内容).
到目前为止,我已经尝试了以下以及其他几个:
React.findDOMNode(this.refs['author']).value; // "value" does not exist on type "Element"
React.findDOMNode(this.refs['author']).getAttribute('value'); // null
React.findDOMNode(this.refs['author']).textContent; // null
Run Code Online (Sandbox Code Playgroud)
在intellisense中我可以看到以下内容,但我仍然无法弄清楚在这里调用什么.
我正在使用DefinitedlyTyped中的类型定义.另外,我是前端开发的新手,所以也许我的方法是错误的.
例如:
class Example
{
public:
explicit Example(int n) : num(n) {}
void addAndPrint(vector<int>& v) const
{
for_each(v.begin(), v.end(), [num](int n) { cout << num + n << " "; });
}
private:
int num;
};
int main()
{
vector<int> v = { 0, 1, 2, 3, 4 };
Example ex(1);
ex.addAndPrint(v);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当您在MSVC2010中编译并运行它时,您会收到以下错误:
错误C3480:'Example :: num':lambda捕获变量必须来自封闭的函数作用域
但是,使用g ++ 4.6.2(预发行版),您将得到:
1 2 3 4 5
根据标准草案哪个编译器是正确的?
这是一些示例代码:
#include <iostream>
#include <vector>
template <typename T>
std::vector<typename T::iterator> f(T t)
{
std::vector<typename T::iterator> v;
for (auto i = t.begin(); i != t.end(); ++i)
{
v.push_back(i);
}
return v;
}
template <typename T>
void print(const std::vector<T>& v)
{
for (auto i = v.begin(); i != v.end(); ++i)
{
std::cout << **i << ' ';
}
std::cout << std::endl;
}
int main()
{
std::vector<int> v{1, 2, 3};
print(f(v));
std::vector<std::vector<int>::iterator> itervec = f(v);
print(itervec);
}
Run Code Online (Sandbox Code Playgroud)
在ideone上输出是:
1 2 3 …
Run Code Online (Sandbox Code Playgroud) 在编译时,C++ 11中是否有一种方法可以将一种类型的数组转换为另一种数据类型:
#include <iostream>
#include <array>
#include <type_traits>
int main()
{
static constexpr std::array<double, 3> darray{{1.5, 2.5, 3.5}};
static constexpr std::array<int, 3> iarray(darray); // not working
// Is there a way to cast an array to another data type ?
return 0;
}
Run Code Online (Sandbox Code Playgroud) 代码示例:
using System;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Console.WriteLine("Start");
Foo f = new Foo();
f.FooAsync();
}
public class Foo
{
public async void FooAsync()
{
try
{
await Task.Run(() =>
{
Console.WriteLine("Throwing");
throw new Exception();
});
}
catch (Exception)
{
Console.WriteLine("Caught");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码从C#小提琴或从我的PC上的控制台运行时打印:
Start
Throwing
Caught
Run Code Online (Sandbox Code Playgroud)
但是,当我从Visual Studio(调试模式下的F5)运行它时,我收到以下消息:
我不明白为什么我从Visual Studio得到"没有在用户代码中处理"消息,尽管从控制台或C#小提琴运行它是好的.我错过了一些明显的东西吗
UPDATE
我已经尝试f.FooAsync().Wait();
在Main中等待任务,但仍然报告异常未处理(相同的错误消息).
struct X {
constexpr static char a1[] = "hello"; // Okay
constexpr static const char* a2[] = {"hello"}; // Error
};
int main(){}
Run Code Online (Sandbox Code Playgroud)
使用gcc编译会出错:
错误:在'{'标记之前,不允许使用括号括起的初始值设定项
这是constexpr的非法使用吗?
编辑
我尝试了3个不同版本的gcc,它在我最新的4.7.0上编译(我刚刚下载它,我使用的是mingw-w64),所以它看起来是一个固定的bug(这个bug的链接会是不过很好!)
4.7.0 20120311(预发行)//好的
4.6.4 20120305(预发布)//错误
4.7.0 20110829(实验性)//错误
VOID是什么意思?这意味着什么都没有,或者意味着什么?
在字典中搜索时,答案是什么都没有.但有些人说这意味着什么.我很困惑它是否与null相同或具有独特的含义.当我在google上搜索它的含义时,我得到了无效指针的结果以及许多我无法理解的东西,最后我是一个12级女孩.
为什么C++标准包含atomic_store
或atomic_load
重载shared_ptr
,但不是weak_ptr
?
这只是一个疏忽,还是有没有提供原子操作的实际原因weak_ptr
?
c++ ×7
c++11 ×5
.net ×1
arrays ×1
async-await ×1
atomic ×1
c# ×1
casting ×1
compile-time ×1
constexpr ×1
containers ×1
g++ ×1
iterator ×1
javascript ×1
lambda ×1
react-jsx ×1
reactjs ×1
shared-ptr ×1
typescript ×1
visual-c++ ×1