我正在尝试读取每行有多个分隔符的文件。下面是数据
2,22:11
3,33:11
4,44:11
5,55:11
6,66:11
7,77:11
8,88:11
9,99:11
10,00:11
11,011:11
12,022:11
13,033:11
14,044:11
15,055:11
16,066:11
17,077:11
18,088:11
19,099:11
Run Code Online (Sandbox Code Playgroud)
代码如下
在这里,我尝试首先使用逗号作为分隔符来读取该行,然后读取冒号。
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::string line;
std::string token;
std::ifstream infile;
infile.open("./data.txt");
while (std::getline(infile, line,',')) {
std::cout << line << std::endl;
while(getline(infile, token, ':'))
{
std::cout << " : " << token;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是代码有一个问题,因为它跳过了第一行。另外,如果我评论第二个 while 循环,则只有第一行被打印,下面是输出
Ian 无法弄清楚代码到底哪里出了问题
输出
2
: 22 : 11
3,33 : 11
4,44 : 11
5,55 : 11 …Run Code Online (Sandbox Code Playgroud) 下面是我遇到的代码,它实际上是std::is_base_of在C++ 11或C++中完成的boost::is_base_of.
辅助函数下面也是这样的:
template<typename D, typename B>
class IsDerivedFromHelper
{
class No { };
class Yes { No no[3]; };
static Yes Test( B* );
static No Test( ... );
public:
enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
};
template <class C, class P>
bool IsDerivedFrom() {
return IsDerivedFromHelper<C, P>::Is;
}
Run Code Online (Sandbox Code Playgroud)
有人能解释一下enum的Is作用吗?
什么是静态Yes Test 和静态No Test?它们是功能调用吗?
下面是具有各种返回语句的代码,并且所有代码都运行良好.编译器会抛出fun_ret_obj1的警告
Test.cpp:在函数'myClass&fun_ret_obj1()'中:Test.cpp:45:警告:返回对局部变量'myObj'的引用
但仍然输出似乎很好.是机会吗?以下任何退货声明是否有任何捕获?
解释会非常有用,谢谢
#include <iostream>
using namespace std;
class myClass {
public:
int a ;
myClass()
{
a = 10;
}
};
myClass& fun_ret_add()
{
myClass *ptr = new myClass();
return *ptr;
}
myClass* fun_ret_ptr()
{
myClass *ptr = new myClass();
return ptr ;
}
myClass fun_ret_obj()
{
myClass myObj;
return myObj;
}
myClass& fun_ret_obj1()
{
myClass myObj;
return myObj;
}
int main()
{
myClass obj,obj1;
std::cout <<"In Main \n";
myClass *a = fun_ret_ptr();
std::cout<<a->a<<"\n";
myClass &b = fun_ret_add(); …Run Code Online (Sandbox Code Playgroud) 下面的代码序列化和反序列化类和结构的成员。序列化正在工作,但我在尝试使用反序列化它时遇到了以下错误oarch >> BOOST_SERIALIZATION_NVP(outObj);
代码中是否有任何我尚未实现和未实现的大遗漏。
在 main.cpp 包含的文件中:1:在 /usr/local/include/boost/archive/binary_oarchive.hpp 中包含的文件中:21:在 /usr/local/include/boost/archive/binary_oarchive_impl.hpp 中包含的文件中: 22:在 /usr/local/include/boost/archive/basic_binary_oarchive.hpp 包含的文件中:33:在 /usr/local/include/boost/archive/detail/common_oarchive.hpp 中包含的文件中:22:在文件中包含/usr/local/include/boost/archive/detail/interface_oarchive.hpp:23:在 /usr/local/include/boost/archive/detail/oserializer.hpp 包含的文件中:68:/usr/local/include/boost /archive/detail/check.hpp:162:5: 错误:static_assert 失败 "typex::value" BOOST_STATIC_ASSERT(typex::value); ^ ~~~~~~~~~~~~ /usr/local/include/boost/static_assert.hpp:70:41: 注意:扩展自宏“BOOST_STATIC_ASSERT”
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <map>
#include <boost/serialization/map.hpp>
#include <boost/serialization/split_member.hpp>
struct values
{
std::string name;
std::string sex;
values():name("dummy"),sex("dummy"){} ;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(name);
ar …Run Code Online (Sandbox Code Playgroud) 我正在阅读 std 模板库书籍,但对 STL 容器章节中列出的以下详细信息感到困惑。显然,它指定了 STD::VECTOR 操作和效果
Operation Effect
vector<Elem> c(c2) | Copy constructor; creates a new vector as a copy of c2 (all elements are copied)
vector<Elem> c = c2 | Copy constructor; creates a new vector as a copy of c2 (all elements are copied)
vector<Elem> c(rv) | Move constructor; creates a new vector, taking the contents of the rvalue rv (since C++11)
vector<Elem> c = rv | Move constructor; creates a new vector, taking the contents of …Run Code Online (Sandbox Code Playgroud) 我有两个数组,哈希保存这些数组
Array 1:
my $group = "west"
@{ $my_big_hash{$group} } = (1534,2341,2322,3345,689,3333,4444,5533,3334,5666,6676,3435);
Array 2 :
my $element = "Location" ;
my $group = "west" ;
@{ $my_tiny_hash{$element}{$group} } = (153,333,667,343);
Run Code Online (Sandbox Code Playgroud)
现在我想比较一下
@ {$ my_tiny_hash {$ element} {$ group}}
同
@ {$ my_big_hash {$ group}}
并检查微小哈希数组的所有元素是否都是big_hash数组的一部分.正如我们所看到的,微小哈希只有3位元素,如果我们只比较前3位数字,所有这些元素都与大哈希匹配
如果前三个数字/字母匹配并且所有都在大数组中可用,那么它的匹配或我们必须打印不匹配的元素
它是一个阵列比较.我们如何实现它.
PS:没有Array Utils,如何实现它
使用Array Utils的解决方案非常简单
my @minus = array_minus( @{ $my_tiny_hash{$element}{$group} } , @{ $my_big_hash{$group} } );
Run Code Online (Sandbox Code Playgroud)
但它比较所有数字,我只想匹配前3位数
希望这很清楚
谢谢
以下是我设计的结构和类,最终目标是使用 boost 序列化将值写入 xml 当我编译下面的代码时,我收到一个编译时错误(错误很大,我只是粘贴了它的最后一行这可能讨论了这个问题)
/boost_1_49_0/include/boost/serialization/access.hpp:118: 错误: 'class std::map, std::allocator >, std::map, std::allocator >, std::map, std::allocator >、IdInfo、std::less、std::allocator > >、std::allocator、std::allocator >、IdInfo> > >、std::less、std::allocator > >、std::allocator、std ::allocator >, std::map, std::allocator >, IdInfo, std::less, std::allocator > >, std::allocator, std::allocator >, IdInfo> > > > > >, std ::less、std::allocator > >、std::allocator、std::allocator >、std::map、std::allocator>、std::map、std::allocator>、IdInfo、std::less , std::allocator > >, std::allocator, std::allocator >, IdInfo> > >, std::less, std::allocator > >, std::allocator, std::allocator >, std::映射、std::allocator >、IdInfo、std::less、std::allocator > >、std::allocator、std::allocator >、IdInfo> > > > > > > >' 没有名为 'serialize' 的成员 …
在main下面的函数中,第一个static_cast是有效的,因为我试图将一个(对Derived1类的基类引用)转换为派生引用但是为什么第二个转换打印带有这些值,虽然它打印了垃圾值d2.y,但我认为它应该是警告或编译错误。对基对象的基引用如何选择派生类的值(d2.y在这种情况下,我也可以为其分配一个值)
有人可以解释一下在这两种情况下会发生什么。
class Base1
{
public:
Base1()
{
x = 999;
}
int x;
};
class Derived1: public Base1
{
public:
Derived1()
{
y = 1000;
}
int y;
};
int main()
{
Derived1 d;
std::cout << d.x << " " << d.y << std::endl;
Base1& b = d;
Base1 b1;
Base1 & b2 = b1;
Derived1& d1 = static_cast<Derived1&>(b);
Derived1& d2 = static_cast<Derived1&>(b2);
std::cout << d1.x << " " …Run Code Online (Sandbox Code Playgroud) 以下是输入和输出详细信息
输入:这只是一个示例输入,实际输入是巨大的,仅一行
[{"mnemonic":"PT.IA1","ID":"000628"}, {"mnemonic":"EOR.1","ID":"000703"}]
Run Code Online (Sandbox Code Playgroud)
代码:我试图通过将定界符设置为}来读取文件,以便获取每个值,但是作为单行文件,它将所有内容打印为一个,我如何通过为此设置定界符来解析此行行,拆分功能足以完成这项工作吗?
our $conf =
{
chunk_separator => '\{,',
}
open( FH, "Etot_Data.txt" ) or die $!;
while ( my $chunk = <FH> ){
my $sections = [ split $conf->{chunk_separator}, $chunk ]
print "$chunk\n";
}
Run Code Online (Sandbox Code Playgroud)
输出量
我想从每个值中选择“ ID”,并在其前添加“ abc”。最终字符串看起来像abc.000628或abc.000703并将其保存在哈希中。除了json字符串中的ID外,不需要其他值。是否可以将json文件作为普通文件读取并对其进行操作。我没有json解析器,也没有使用它的选项
谢谢您的帮助
下面是代码
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
my $dbh = DBI->connect('dbi:DBM:sid=upr18;host=abd-up3db1',"user", "pass");
my $sth = $dbh->prepare("SELECT count(*) FROM temp_table");
$sth->execute();
Run Code Online (Sandbox Code Playgroud)
虽然connnect和prepare语句实际上不会抛出错误,并且执行语句错误输出.以下是错误
DBD :: DBM :: st执行失败:无法打开./temp_table.lck:/usr/perl5/vendor_perl/5.8.4/i86pc-solaris-64int/DBD/File.pm第574行没有此类文件或目录.[ for Test.pl第8行的语句"SELECT count(*)FROM temp_table"].
我出错的任何建议.