小编Les*_*ite的帖子

使用标准库替换strtok循环

我有一个strtok解决的问题(从字符串中拆分子串)但我意识到strtok不安全.我想使用C++标准库的一些更现代的部分.

我应该用什么呢?

static int ParseLine(std::string line, 
                     std::string seps, 
                     int startIdx, 
                     std::vector<CNode>& collection)
{
    if (startIdx > collection.size())
    {
        throw std::invalid_argument("the start index is out of range");
    }
    char buf[2000];
    strcpy_s(buf, line.c_str());
    auto idx = startIdx;
    for (auto objectType = strtok(buf, seps.c_str()); objectType != nullptr; idx++)
    {
        if (idx == collection.size())
        {
            collection.push_back(CNode(idx));
        }
        collection[idx].SetObjectType(objectType);
        objectType = strtok(nullptr, seps.c_str());
    }
    return (idx - 1);
}
Run Code Online (Sandbox Code Playgroud)

这里是一个用_CRT_SECURE_NO_WARNINGS编译的完整示例:

#include <string>
#include <vector>
#include <iostream>

class CObject
{
    std::string _objectType;

public:
                CObject() : _objectType("n/a") …
Run Code Online (Sandbox Code Playgroud)

c++ stl

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

从C#行中提取GUID

我尝试使用IndexOf简化一些遗留代码,以从行中检索GUID。我可以进一步简化下面的代码以摆脱使用guids.Any和guids.First吗?

// Code using regular expression
private static string RetrieveGUID2(string[] lines)
{
    string guid = null;
    foreach (var line in lines)
    {
        var guids = Regex.Matches(line, @"[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?")
            .Cast<Match>().Select(m => m.Value);
        if (guids.Any())
        {
            guid = guids.First();
            break;
        }
    }
    return guid;
}
Run Code Online (Sandbox Code Playgroud)

下面是编译示例中给出的旧版代码:

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var lines = new[]
            {
                "</ItemGroup>\n",
                "<PropertyGroup\n",
                "Label = \"Globals\">\n",
                "<ProjectGuid>{A68615F1-E672-4B3F-B5E3-607D9C18D1AB}</ProjectGuid>\n",
                "</PropertyGroup>\n"
            };

            Console.WriteLine(RetrieveGUID(lines));
            Console.WriteLine(RetrieveGUID2(lines));
        }

        // Legacy …
Run Code Online (Sandbox Code Playgroud)

c#

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

如何使用C++ std来计算整数集合的前导零和尾随零

我们可以通过调用标准库来替换循环来计算整数集合中的前导零吗?

我正在学习std,但由于我需要知道前一个元素,所以无法想办法使用count或count_if之类的东西.

int collection[] = { 0,0,0,0,6,3,1,3,5,0,0 };
auto collectionSize = sizeof(collection) / sizeof(collection[0]);

auto countLeadingZeros = 0;
for (auto idx = 0; idx < collectionSize; idx++)
{
    if (collection[idx] == 0)
        countLeadingZeros++;
    else
        break;
}
// leading zeros: 4*0
cout << "leading zeros: " << countLeadingZeros << "*0" << endl;
Run Code Online (Sandbox Code Playgroud)

我有一个类似的案例来统计同一个集合中的尾随零.

auto countTrailingZeros = 0;
for (auto idx = collectionSize - 1; idx >= 0; idx--)
{
    if (collection[idx] == 0)
        countTrailingZeros++;
    else
        break;
}
// trailing zeros: 2*0 …
Run Code Online (Sandbox Code Playgroud)

c++ stl

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

基于范围的循环不适用于数组的指针

为什么基于范围的for循环不适用于数组的指针?

auto pCollection = new int[3] { 0,1,2 };
// error C3312: no callable 'begin' function found for type 'int *'
for (auto value : pCollection)
{
    std::cout << value << std::endl;
}
delete[] pCollection;
Run Code Online (Sandbox Code Playgroud)

但可以在数组上使用:

int collection[3]{ 0,1,2 };
for (auto value : collection)
{
    std::cout << value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays

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

标签 统计

c++ ×3

stl ×2

arrays ×1

c# ×1