AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[c++17](coding)

Martin Hope
SeedlessKiwi
Asked: 2025-01-19 09:47:54 +0800 CST

有没有办法使用 XML boost::property_tree 读取无限时间长度?

  • 6

我正在尝试使用 boost::property_tree 从 XML 配置文件中读取某些配置的无限时间持续时间(其他配置为非无限时间持续时间)。此代码适用于非无限持续时间。

我的代码读取的值:

boost::property_tree::ptree property_tree;
boost::property_tree::read_xml(filepath, property_tree);
auto config = property_tree.get_child("Config");
std::chrono::seconds my_duration_value = std::chrono::seconds(config.get<unsigned int>("MyDurationField"));

我希望能够在配置文件中执行以下操作:

  <Config>
     <MyDurationField>inf</MyDurationField>
  </Config>

有没有一种方法可以做到这一点,既适用于无限时间,也适用于非无限时间?提前致谢。

c++17
  • 1 个回答
  • 20 Views
Martin Hope
nore
Asked: 2025-01-14 06:20:54 +0800 CST

使用转义序列时,什么原因导致 printf() 向控制台输出奇怪的字符?[重复]

  • 3
此问题这里已有答案:
如何使用 printf 与 std::string (9 个答案)
16 小时前关闭。

我正在编写一个简单的函数,用于我的一个实用程序实现文件。我最近发现——在使用其中一个 C++17 库时——使用对的std::filesystem::path::string()调用中的函数输出目录条目printf()只会导致将一串奇数字符发送到STDOUT。使用cout结果没有问题。代码如下:

    if( !initialized )
    {
        try
        {
            const filesystem::path MODELS_DIRECTORY = R"(C:\-----\-----\-----\models)";
            const filesystem::path RESOURCES_DIRECTORY = filesystem::relative(R"(\Resources)", MODELS_DIRECTORY);

            for( const filesystem::directory_entry& dir_entry : filesystem::directory_iterator{ MODELS_DIRECTORY } )
            {
                string test = "this\\is\\a test\\";
                string directory = dir_entry.path().string();
                printf("%s\n", test);
                //cout << directory << endl;
            }
        }
        catch( filesystem::filesystem_error& fs_err )
        {
            printf( "fs exception caught in GraphicsDataCatalog::InitCatalog()" );
        }
        initialized = true;
    }

使用由 组成的测试机制std::string test并调用printf()表明双反斜杠是罪魁祸首。删除字符串中的空格并不能解决问题——我假设也许有一个格式说明符可以解决打印到控制台的不稳定字符。使用cout调用string返回的dir_entry.path().string()可以成功。

有人对这个主题有更多的了解吗?

MRE(最小可重复示例):

#include <iostream>

using namespace std;

int main()
{
    const string PASS_STRING = "This is a test.";
    const string FAIL_STRING = "This\\is not\\a\\test.";

    cout << PASS_STRING << endl;
    cout << "Test passed.\n" << endl;

    printf( "%s\n", FAIL_STRING );
    printf( "Test failed.\n" );

    return 0;
}
c++17
  • 1 个回答
  • 29 Views
Martin Hope
AK1557
Asked: 2024-11-17 23:16:11 +0800 CST

我想了解为什么这段代码运行得这么慢

  • 6

我已经编写了代码,也许是一个简单的编码测试。(详情如下)

拳击手进行比赛

更优秀的人总是会赢 例如)如果 A 拳击手比 B 更优秀,那么 A 总是会赢 B 如果 C 拳击手比 A 更优秀,那么 C 总是会赢 A、B

So, always (C > A > B), never (C > A > B > C) things.

比赛结束后,部分比赛结果缺失。根据现有结果,确定有多少名拳击手可以确定排名。

*constrains
i)  1 <= boxers <= 100.
ii)  1 <= results.size() <= 4500, results contains match_data.
iii) match_data [A, B] means (A defeated B).
iV) no contradictions in the data: if [A,B] exist, [B,A] does not.

----------------------------------------------------------------------

N of boxer                       results                          answer

    5             [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]]      2

-----------------------------------------------------------------------

                        [[  explain  ]]

在这种情况下,您可以构建两个排名链。

4 > 3 > 2 > 5 1 > 2 > 5

这里,2、5 可以分别确认他们的排名为第 4、第 5,但是,4、3、1 由于缺少数据而无法确认他们的相对排名。

因此答案是2;

下面是我的代码

#include <vector>
#include <queue>
#include <iostream>

using namespace std;

int win = 0;
int lose = 1;

int solution(int n, vector<vector<int>> results) {

    queue<pair<int, int>> q;

    vector<vector<vector<int>>> Graph(n, vector<vector<int>>(2));

    vector<int> rating = vector(n,0);

    vector<vector<vector<int>>> List(n, vector<vector<int>>(2, vector<int>(n, 0)));


    for(int match=0; match < results.size(); match++){

        int winner = results[match][win]-1;
        int loser = results[match][lose]-1;

        Graph[winner][win].push_back(loser);
        Graph[loser][lose].push_back(winner);
    
    }

    for(int index=0; index<n; index++){
    
        if(Graph[index][win].size() == 0 && Graph[index][lose].size() == 0) return 0;
    
        if(Graph[index][win].size() == 0){
            for(int i=0; i<Graph[index][lose].size(); i++){
                q.push({Graph[index][lose][i], index});
            }
            while(!q.empty()){
                auto [winner, loser] = q.front();
                q.pop();
            
                if(List[winner][win][loser]==0){
                    List[winner][win][loser] = 1;
                    rating[winner]++;
                }
            
            
                for(int j=0; j<n; j++){
                    if(List[loser][win][j] == 1 && List[winner][win][j] == 0){
                        List[winner][win][j] = 1;
                        rating[winner]++;
                    }
                }
            
                for(int j=0; j<Graph[winner][lose].size(); j++){
                    q.push({Graph[winner][lose][j], winner});
                }
            }
        }
    
        if(Graph[index][lose].size() == 0){
            for(int i=0; i<Graph[index][win].size(); i++){
                q.push({Graph[index][win][i], index});
            }
         
            while(!q.empty()){
                auto [loser, winner] = q.front();
                q.pop();
                
                if(List[loser][lose][winner]==0){
                    List[loser][lose][winner] = 1;
                    rating[loser]++;
                }
                
            
                for(int j=0; j<n; j++){
                    if(List[winner][lose][j] == 1 && List[loser][lose][j] == 0){
                        List[loser][lose][j] = 1;
                        rating[loser]++;
                    }
                }
            
                for(int j=0; j<Graph[loser][win].size(); j++){
                    q.push({Graph[loser][win][j], loser});
                }
            }
        }
    }

    int answer = 0;
    for(int index=0; index<n; index++){
        //cout << rating[index] << ", ";
        if(rating[index]==n-1){
            answer++;
        }
    }

    return answer;

}

我的代码可以运行,并且我预计代码复杂度为 O(n^2+n⋅m) << n = boxer number, m = results.size() >>

当输入大量数据时,它仍然有效,但需要很长时间。

我发现 Floyd-Warshall 算法效果更好。

即便如此,我还是真的很想知道为什么我的代码花费了这么多时间以及它的实际时间复杂度是多少。

  1. 感谢您花时间阅读并提供帮助。
c++17
  • 2 个回答
  • 77 Views
Martin Hope
PinkTurtle
Asked: 2024-01-03 03:25:04 +0800 CST

来自 time_t 的 std::filesystem::file_time_type - 如何?

  • 5

我想std::filesystem::file_time_type从 a创建 a std::time_t,但不知道如何做。

例子:

time_t t = 1337;
std::filesystem::file_time_type ft = ...; //how?

理想情况下,我希望它能够与 c++17 一起使用,但我也会采用 c++20 解决方案。

c++17
  • 1 个回答
  • 18 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve