小编Moh*_*oun的帖子

Windows窗体文本框输入密钥

我有一个TextBox,但我找不到任何来源解释当按下按钮时如何调用函数.

public Simple()
{
    Text = "Server Command Line";
    Size = new Size(800, 400);

    CenterToScreen();

    Button button = new Button();
    TextBox txt = new TextBox ();

    txt.Location = new Point (20, Size.Height - 70);
    txt.Size = new Size (600, 30);
    txt.Parent = this;

    button.Text = "SEND";
    button.Size = new Size (50, 20);
    button.Location = new Point(620, Size.Height-70);
    button.Parent = this;
    button.Click += new EventHandler(Submit);
}
Run Code Online (Sandbox Code Playgroud)

一些消息来源告诉我使用一个函数,但我不明白它是如何被调用的.

c# textbox winforms

14
推荐指数
2
解决办法
3万
查看次数

确定图形是否包含三角形?

如果我们的目标时间复杂度是O(| V |*| E |)或O(V ^ 3)等,则该问题具有简单的解决方案.但是,我的教授最近给了我们一个问题陈述的任务:

设G =(V,E)为连通无向图.编写一个算法,确定G是否包含O(| V | + | E |)中的三角形.

在这一点上,我很难过.维基百科说:

可以测试具有m个边的图在时间O(m ^ 1.41)中是否是无三角形的.

除了在Quantum计算机上运行的算法之外,没有提到更快算法的可能性.之后我开始诉诸更好的消息来源.关于Math.SE的一个问题将我与本文联系起来说:

已知用于查找和计数三角形的最快算法依赖于快速矩阵乘积并且具有O(n ^ω)时间复杂度,其中ω<2.376是快速矩阵乘积指数.

而这就是我开始意识到的可能,我们被欺骗了解决一个未解决的问题!那个卑鄙的教授!

但是,我仍然有点怀疑.该文件称"寻找和计算".这相当于我试图解决的问题吗?

TL; DR:我被愚弄了,还是我忽视了一些如此微不足道的事情?

algorithm graph-theory

11
推荐指数
1
解决办法
1682
查看次数

推广这些代码行?

在编程时,我遇到了一些带有代码的墙.它看起来像这样:

〜颜色!〜

这就是问题所在.我拍了一张漂亮的截图来减轻我的内疚.漂亮的颜色无法弥补缺乏可维护性.我几乎不知道如何概括这样的代码.

我尝试了什么?

好吧,考虑第3和第6个参数的周期性.它也与其他参数的周期性一致.如果我们使用数组,这样的东西将允许我们将此代码转换为具有9行的循环.这是一个改善,因为我们下降了66%.但是,这还不够好.如果将其更改为1行,那将是最好的.这将至少使它有点更易于维护.

这真的是个问题吗?

好吧,让我们这样说吧.那里的代码可能也是错的.

c++ redundancy c++11 c++14

9
推荐指数
1
解决办法
367
查看次数

如何真正删除矢量

我对C++内存管理很陌生,因为与C不同,释放所有内存还有更多障碍.

我试图成功删除任何类型的矢量指针(即矢量*数据)

/** 
 * We test the program in accessing vectors
 * with dynamic storage
**/
#include <iostream>
#include <vector> // you must include this to use vectors

using namespace std;

int main (void){
    // Now let's test the program with new and delete
    // for no memory leaks with vectors (type safe)
    // however, just calling delete is not enough to free all memory
    // at runtime
    vector <int> * test_vect = new vector <int> (10,5);

    // Print out …
Run Code Online (Sandbox Code Playgroud)

c++ memory-leaks memory-management vector

8
推荐指数
2
解决办法
2万
查看次数

2个线程比1慢?

我正在玩耍,std::thread出现了一些奇怪的东西:

#include <thread>

int k = 0;

int main() {
    std::thread t1([]() { while (k < 1000000000) { k = k + 1; }});
    std::thread t2([]() { while (k < 1000000000) { k = k + 1; }});

    t1.join();
    t2.join();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在使用clang ++编译上面的代码而没有优化时,我得到了以下基准:

real 0m2.377s  
user 0m4.688s  
sys  0m0.005s
Run Code Online (Sandbox Code Playgroud)

然后我将代码更改为以下内容:(现在只使用1个线程)

#include <thread>

int k = 0;

int main() {
    std::thread t1([]() { while (k < 1000000000) { k = k + 1; }});

    t1.join();

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ performance multithreading c++11 stdthread

7
推荐指数
1
解决办法
1027
查看次数

原始调用者被销毁后,std :: thread :: detach导致崩溃

struct Test {
    bool active{true};

    void threadedUpdate() {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
        if(!active) // crashes here after Test instance is destroyed
            return; 
    }

    Test() { 
        std::thread([this]{ while(true) threadedUpdate(); }).detach();
    }

    ~Test() { 
        // somehow stop the detached thread?
    } 
};
Run Code Online (Sandbox Code Playgroud)

Test初始化实例时,它会生成并分离std::thread在后台运行的实例.当同一个实例被销毁时,前面提到的线程试图访问该active成员,该成员与实例一起被销毁,导致崩溃(和一个AddressSanitizer回溯).

有没有办法停止分离的线程~Test()

设计很糟糕.如果正确生成/处理在调用者被销毁之前,如何在后台运行线程?

c++ multithreading detach c++11 stdthread

7
推荐指数
2
解决办法
2962
查看次数

c ++ lambda删除偶数

我正在学习C++ lambda函数,这个可能很容易的问题就是给我一些问题.

我有一个带有一些整数的向量.我想删除所有偶数.

我目前已经删除了偶数,但是当我打印向量的内容时,我最后还是在向量中留下了一些重复的数据.

int main()
{
  std::vector<int> v1 = {0,1,1,2,3,5,8,13,21,55,89};

  for (const auto &i : v1) std::cout<< i << " ";
  std::cout<<std::endl; 

  v1.erase(std::remove_if(v1.begin(), v1.end(), [](int i)
       { return (i % 2) == 0; }));

  for (const auto &i : v1) std::cout<< i << " ";
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

0 1 1 2 3 5 8 13 21 55 89 
1 1 3 5 13 21 55 89 55 89
Run Code Online (Sandbox Code Playgroud)

我希望第二行的输出为:

1 1 3 5 13 21 55 89
Run Code Online (Sandbox Code Playgroud)

编辑

感谢大家.我使用了错误版本的擦除(现在觉得非常愚蠢).这是正确的代码: …

c++ lambda vector c++11

3
推荐指数
1
解决办法
897
查看次数

从edittext获取十进制数

我知道类似的东西已经被问过了,但是我遇到一些麻烦来得到一个来自键盘的十进制数字.

我在onCreate方法中的Java代码应该是:

textS0 = (EditText)findViewById(R.id.editS0);
Button btn_S0 = (Button)findViewById(R.id.getS0);

btn_S0.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v)
    {
        //how should I get here the number from keyboard?
        //I think  I should be something like
        //double S0=textS0.getText()....
    }
});
Run Code Online (Sandbox Code Playgroud)

这就是我的XML文件所包含的内容

<EditText
       android:id="@+id/editS0"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:ems="10"
       android:hint="@string/S0"
       android:inputType="numberDecimal" />
<Button
       android:id="@+id/getS0"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/setS0" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-edittext

2
推荐指数
1
解决办法
2万
查看次数

C语言中函数执行的优先顺序

以下语句的执行顺序是什么:

x = f(2) * g(5) + h();
Run Code Online (Sandbox Code Playgroud)

我看过这个链接,优先顺序应该是f,g和h.我是对还是没有,请解释一下

c c++ operator-precedence

2
推荐指数
1
解决办法
192
查看次数

如何将2位数字分成单个数字?

程序提示用户输入2位十进制数.用户输入后,如何将数字分成两个单独的变量?

后来我需要使用数字的第一部分和第二部分,因此它们需要处于不同的变量中.

c++ integer digit

1
推荐指数
1
解决办法
4029
查看次数