我是 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) 我正在尝试制作一个包含 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# 的对象初始化顺序是这样的:
下面你会看到一个简单的测试程序和它在我运行时产生的输出。
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)