GVKun编程网logo

如何将std :: vector 转换为C风格的原始数据(无符号字符**)?(stdstring转char)

6

本文将分享如何将std::vector转换为C风格的原始数据的详细内容,并且还将对无符号字符**?进行详尽解释,此外,我们还将为大家带来关于.net–如何将System::String^转换为std:

本文将分享如何将std :: vector 转换为C风格的原始数据的详细内容,并且还将对无符号字符**?进行详尽解释,此外,我们还将为大家带来关于.net – 如何将System :: String ^转换为std :: string?、.net – 将System :: array转换为std :: vector、c 11 – 如何将std :: chrono :: time_point转换为字符串、c – std :: list,std :: vector方法和malloc()的相关知识,希望对你有所帮助。

本文目录一览:

如何将std :: vector <std :: byte>转换为C风格的原始数据(无符号字符**)?(stdstring转char)

如何将std :: vector 转换为C风格的原始数据(无符号字符**)?(stdstring转char)

要向接受vector<char>的C函数提供const char**,您可以访问内部缓冲区并返回指向它的指针:

#include <iostream>
#include <vector>

void someCApi(const char** c,size_t len)
{
    for (size_t i=0; i<len; ++i)
    {
        std::cout << static_cast<int>((*c)[i]) << std::endl;
    }
}

auto main() -> int
{
    std::vector<char> buff{1,3,5,7,11,13,17,19};
    const char* buffPtr = buff.data();
    someCApi(&buffPtr,buff.size());

    return 0;
}

https://onlinegdb.com/B1BSNKyrv

用于获取指向数组的指针的简单接口:

template <typename T>
class pptr
{
    const T* buff=nullptr;
public:
    ppta(std::vector<T>& v): buff(v.data()){};
    const T** operator()(){return &buff;}
};

//Usage
someCApi(pptr(buff)(),buff.size());

.net – 如何将System :: String ^转换为std :: string?

.net – 如何将System :: String ^转换为std :: string?

所以我在clr中工作,在visual c中创建.net dll.

我这样的代码:

static bool InitFile(System::String^ fileName,System::String^ container)
{
    return enc.InitFile(std::string(fileName),std::string(container));
}

有编码器,normaly resives std :: string.但是如果我从std :: string和C2440中删除通常相同的参数,那么编译器(visual studio)会给出C2664错误. VS告诉我它无法将System :: String ^转换为std :: string.

所以我很伤心……我该怎么做才能将System :: String ^变成std :: string?

更新:

现在有了你的帮助,我有了这样的代码

#include <msclr\marshal.h>
#include <stdlib.h>
#include <string.h>
using namespace msclr::interop;
namespace NsstW
{
  public ref class CFEW
  {
 public:
     CFEW() {}

     static System::String^ echo(System::String^ stringToReturn)
    {
        return stringToReturn;  
    }

     static bool InitFile(System::String^ fileName,System::String^ container)
    {   
        std::string sys_fileName = marshal_as<std::string>(fileName);;
        std::string sys_container = marshal_as<std::string>(container);;
        return enc.InitFile(sys_fileName,sys_container);
    }
...

但是当我尝试编译时,它给了我C4996

错误C4996:’msclr :: interop :: error_reporting_helper< _To_Type,_From_Type> :: marshal_as’:库不支持此转换,或者不包括此转换所需的头文件.有关添加自己的编组方法的信息,请参阅“如何:扩展封送库”的文档.

该怎么办?

解决方法

如果您使用的是VS2008或更新版本,则可以使用 automatic marshaling added to C++非常简单地执行此操作.例如,您可以通过 marshal_as将System :: String ^转换为std :: string:
System::String^ clrstring = "CLR string";
std::string stdString = marshal_as<std::string>(clrstring);

这与用于P / Invoke调用的编组相同.

.net – 将System :: array转换为std :: vector

.net – 将System :: array转换为std :: vector

有没有人知道将CLI / .NET System ::数组转换为C std :: vector的简单方法,除了按元素方式进行操作外?

我正在CLI / C中编写一个包装器方法(下面的SetLowerBoundsWrapper),它接受一个System :: array作为参数,并将等效的std :: vector传递给本机C方法(set_lower_bounds).目前我这样做如下:

using namespace System;

void SetLowerBoundsWrapper(array<double>^ lb)
{
    int n = lb->Length;
    std::vector<double> lower(n); //create a std::vector
    for(int i = 0; i<n ; i++)
    {
        lower[i] = lb[i];         //copy element-wise
    } 
    _opt->set_lower_bounds(lower);
}

解决方法

另一种方法是让.NET BCL代替C标准库来完成工作:
#include <vector>

void SetLowerBoundsWrapper(array<double>^ lb)
{
    using System::IntPtr;
    using System::Runtime::InteropServices::Marshal;

    std::vector<double> lower(lb->Length);
    Marshal::copy(lb,IntPtr(&lower[0]),lb->Length);
    _opt->set_lower_bounds(lower);
}

编辑(回应对Konrad答案的评论):

以下两者都使用VC 2010 SP1为我编译,并且完全等效:

#include <algorithm>
#include <vector>

void SetLowerBoundsWrapper(array<double>^ lb)
{
    std::vector<double> lower(lb->Length);
    {
        pin_ptr<double> pin(&lb[0]);
        double *first(pin),*last(pin + lb->Length);
        std::copy(first,last,lower.begin());
    }
    _opt->set_lower_bounds(lower);
}

void SetLowerBoundsWrapper2(array<double>^ lb)
{
    std::vector<double> lower(lb->Length);
    {
        pin_ptr<double> pin(&lb[0]);
        std::copy(
            static_cast<double*>(pin),static_cast<double*>(pin + lb->Length),lower.begin()
        );
    }
    _opt->set_lower_bounds(lower);
}

(人工范围是允许pin_ptr尽早解除内存,以免阻碍GC.)

c 11 – 如何将std :: chrono :: time_point转换为字符串

c 11 – 如何将std :: chrono :: time_point转换为字符串

如何将std :: chrono :: time_point转换为字符串?
例如:“201601161125”.

解决方法

最灵活的方法是将其转换为struct tm,然后使用strftime(就像sprintf一样).就像是:
std::chrono::system_clock::time_point Now = std::chrono::system_clock::Now();std::time_t Now_c = std::chrono::system_clock::to_time_t(Now);std::tm Now_tm = *std::localtime(&Now_c);/// Now you can format the string as you like with `strftime`

查找strftime here的文档.

如果您有localtime_s或localtime_r可用,则应优先使用localtime.

还有很多其他方法可以做到这一点,但是,虽然大多数更容易使用,但这些方法会产生一些预定义的字符串表示.您可以在功能中“隐藏”上述所有内容以方便使用.

c – std :: list,std :: vector方法和malloc()

c – std :: list,std :: vector方法和malloc()

在中断处理程序下使用stl:list和stl :: vector类型我想避免malloc()调用.

问题:在STL列表和向量中阻止malloc()调用的最佳方法是什么?是否足以创建具有预定义大小的结构,然后避免推/弹/擦除调用?

先感谢您

解决方法

作为推荐:我们在我的工作场所使用其他答案中提到的两种方法:

>自定义分配器:对于我们的内存泄漏跟踪系统,我们的仪器分析器和一些其他系统,我们使用提供的分配器预分配和/或“池”(参见例如boost :: pool)分配 – 通常用于std :: set或std :: map,但std :: list的原理相同.
> reserve / resize:对于std :: vectors,我们提前保留或调整大小(差异很重要,但两者都可以帮助避免将来的分配)是非常常见的做法.

大多数情况下,我们会做这两件事以避免碎片,减少分配器开销,消除复制增加的惩罚等.但有时(特别是对于仪表分析器)我们希望绝对避免在中断处理程序中进行分配.

但是,通常我们会以其他方式避免中断和分配问题:

>进入/退出:尽量避免在中断期间执行除标志或普通副本之外的任何操作;有时静态(或预分配)缓冲区是比STL容器好得多的解决方案.持续中断时间过长通常会导致灾难.
>在alloc / free期间禁用中断:在我们分配/释放时,中断排队,而不是立即调度 – 这是我们正在使用的cpu的一个功能.结合有选择地增加禁用/排队范围的策略(例如std :: list操作),我们有时可以使用中断处理程序作为生产者,一切 – 其他作为消费者模型,而不是覆盖分配器.如果我们正在从std :: list中消费某些东西(例如从网络硬件接收到的消息),那么中断会在尽可能短的时间内排队,同时弹出我们即将处理的内容的副本.

请注意,无锁数据结构可以替代此处的第二个项目符号,我们还没有设置和完成分析以查看它是否有用.无论如何,设计自己的业务是棘手的事情.

对于中断处理程序来说,偏执狂是更好的部分:如果你不确定你所做的事情会起作用,那么以一种完全不同的方式解决问题有时会好得多.

我们今天的关于如何将std :: vector 转换为C风格的原始数据无符号字符**?的分享就到这里,谢谢您的阅读,如果想了解更多关于.net – 如何将System :: String ^转换为std :: string?、.net – 将System :: array转换为std :: vector、c 11 – 如何将std :: chrono :: time_point转换为字符串、c – std :: list,std :: vector方法和malloc()的相关信息,可以在本站进行搜索。

本文标签: