小编Sid*_*eus的帖子

为什么 C++ 正则表达式这么慢?

我是 C++ 新手,必须处理文本文件。我决定用正则表达式来做到这一点。我想出的正则表达式:

(([^\\s^=]+)\\s*=\\s*)?\"?([^\"^\\s^;]+)\"?\\s*;[!?](\\w+)\\s*
Run Code Online (Sandbox Code Playgroud)

我已经根据以下帖子编写了我的 C++ 代码:

c ++ regex 使用 regex_search() 提取所有子字符串

这是 C++ 代码:

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <chrono>
#include <iterator>

void print(std::smatch match)
{
}

int main()
{
    std::ifstream file{ "D:\\File.txt" };
    std::string fileData{};

    file.seekg(0, std::ios::end);
    fileData.reserve(file.tellg());
    file.seekg(0, std::ios::beg);

    fileData.assign(std::istreambuf_iterator<char>(file), 
    std::istreambuf_iterator<char>());

    static const std::string pattern{ "(([^\\s^=]+)\\s*=\\s*)?\"? 
    ([^\"^\\s^;]+)\"?\\s*;[!?](\\w+)\\s*" };
    std::regex reg{ pattern };
    std::sregex_iterator iter(fileData.begin(), fileData.end(), reg);
    std::sregex_iterator end;

    const auto before = std::chrono::high_resolution_clock::now();

    std::for_each(iter, end, print);

    const auto after = …
Run Code Online (Sandbox Code Playgroud)

c++ regex

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

为什么我的依赖属性绑定没有按预期工作?

我正在尝试制作一个包含 ListView 的自定义 UserControl。我想将 SelectedItem 属性绑定到我的视图模型。因此,我在用户控件中创建了一个 DP,并将 ListView 的 SelectedItem 属性绑定到新的依赖属性。

        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
            "SelectedItem",
            typeof(object), 
            typeof(DragAndDropListView),
            new PropertyMetadata(SelectedItemPropertyChangedCallback));

        private static void SelectedItemPropertyChangedCallback(DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
        {
            if (d is DragAndDropListView dragAndDropListView)
            {
            }
        }

        public object SelectedItem
        {
            get => GetValue(SelectedItemProperty);
            set => SetValue(SelectedItemProperty, value);
        }
Run Code Online (Sandbox Code Playgroud)

然后在 xaml 代码中我进行了绑定

            <ListView Name="ListView"
                      PreviewMouseLeftButtonDown="ListViewPreviewMouseLeftButtonDown"
                      AllowDrop="True"
                      MouseMove="ListViewMouseMove"
                      DragEnter="ListViewDragEnter"
                      Drop="ListViewDrop"
                      SelectedItem="{Binding Path= SelectedItem}"/>
Run Code Online (Sandbox Code Playgroud)

为了确保它有效,我将 UserControl 作为 DataContext 分配给 ListView

        public DragAndDropListView()
        {
            InitializeComponent();
            ListView.ItemsSource = Items;
            ListView.DataContext = this; …
Run Code Online (Sandbox Code Playgroud)

c# wpf binding dependency-properties mvvm

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

我的 C# 程序没有按照我认为的对象初始化顺序初始化对象。为什么?

我相信 C# 的对象初始化顺序是这样的:

  • 派生静态字段
  • 派生静态构造函数
  • 派生实例字段
  • 基本静态字段
  • 基本静态构造函数
  • 基本实例字段
  • 基础实例构造函数
  • 派生实例构造函数

下面你会看到一个简单的测试程序和它在我运行时产生的输出。

    public class UiBase
    {
        protected static string Something = "Hello";

        public UiBase()
        {
            Console.WriteLine(this.ToString());
        }
    }

    public class Point : UiBase
    {
        private int X = -1;
        private int Y = -1;

        static Point()
        {
            Something = "Bar";
        }

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }

        public override string ToString()
        {
            return $"Point:{X}/{Y}/{Something}";
        }
    }

    public static class Program{
    public static void …
Run Code Online (Sandbox Code Playgroud)

.net c# initialization

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

标签 统计

c# ×2

.net ×1

binding ×1

c++ ×1

dependency-properties ×1

initialization ×1

mvvm ×1

regex ×1

wpf ×1