Redian新闻
>
C++里面如何最方便的表示这个数组的数组?
avatar
C++里面如何最方便的表示这个数组的数组?# Programming - 葵花宝典
P*p
1
【 以下文字转载自 LeisureTime 讨论区 】
发信人: Penmanship (pencil), 信区: LeisureTime
标 题: [果子店狗血穿越] 来自五代的你 (11)春色连天
发信站: BBS 未名空间站 (Sun Feb 23 19:54:50 2014, 美东)
[果子店狗血穿越] 来自五代的你 (11)春色连天
果子心中这个恨啊,心想这个牛津麻拉可真是的,本想指望他逃过这一劫,不料反被他
连累。好在耶律西拉见她是个女子,又手无寸铁,也不打算难为她,一招手,有士兵给
牵来一匹马。果子咬了咬牙,扶着马鞍要上,不想身轻如燕,麻利地就上了马背,连耶
律西拉也不由得多看了她几眼。果子暗喜,心说铅笔头给的小药丸果然起作用了。
耶律西拉呼哨一声,这队契丹士兵立刻将果子裹挟在马队中间,往大营跑去。进了大营
,果子只见到处有一些装束鲜艳的契丹人在跑了跑去,看来是在为祭祀做准备。果子知
道,这契丹部族的传说,说的是最早的时候,有一个仙女,驾着青牛车闲逛,在木叶山
下遇到一个骑着白马的神人,二人一见钟情,结合生子,开启了契丹族。从此以后,契
丹每到重大节日,尤其是春秋和行军的时候,都要用青牛白马祭祀。
这契丹说起来,在宗教方面本也是开放的,尤其是辽国立国以后,佛教很是兴盛,但这
传统的萨满巫师祭祀,却依旧保留了下来,尤其在保守的契丹贵族之中,更是视为神圣
的传统。果子心想落在这样的祭司手里,可是大大不妙。
她正想着,骑兵队已经簇拥着她穿过了大营,往更远处的一座营寨跑去。这个营寨看起
来防守没那么严密,门口只有两个轻装的守卫。但这些契丹人似乎对这个大寨很是敬重
,到了门口就纷纷下马,也不肯进这个大寨一步。只有耶律西拉一个人推着果子往大寨
里走去,而她也根本不带任何的武器。
果子的好奇心起来了,不知道这里住的是什么神秘人物?正想着,已经到了一个帐篷门
口,只见从里面走出一个侍女模样的人,见了西拉,施礼道:“于越大人,萨满正在冥
想,这人是?”
“是我抓的一个南人奸细,”西拉笑道:“和萨满大人以前都说好了,抓到了就送过来
,哦,对了,这个人自称是牛津麻拉的妹妹。”那侍女点点头,又施礼道:“那我转交
给萨满大人吧。”西拉点点头,对果子喝道:“你老实点。”说完径直走了。
那侍女引着果子进到了屋子里,却也不管她,只是说道:“你在这里自便,只是不要出
这个大账。”说完居然也走了,并没有给她上什么捆绑。
果子大奇,不过她现在也没逃跑的心思,她刚刚穿越过来,又能跑到什么地方去呢?她
四下看了看,却发现这帐篷在外面看着不大,里面居然是相当宏伟。除了中心的火炉外
,帐篷里装饰的非常简单,只是铺了地毯而已。帐篷的另一面有光线进来,看来还有一
个后门。
果子见帐篷里并无第二个人,当下蹑手蹑脚地走到后门去,挑开门帘一看,却不由得喝
了声彩。只见这后门外面,赫然是一个小小的河湾,芳草清清,春花漫野,一派春色连
天,简直就是个世外桃源。果子心里暗赞了一句,不知这帐篷的主人是什么人物,居然
在这战火连天的时刻挑了这么一个地方,也算是个高人了。
只见河边上有一座临时搭建的小亭子,亭子里隐隐约约有个人影。果子暗想,莫非这就
是那个冥想的萨满?当时再也忍耐不住好奇心,悄悄向亭子走去。
不一会的工夫就走到了近前,只见这亭子就修建在水边,视野倒是开阔,可以看到整个
滹沱河的两岸。亭子中间端坐着一人,正直直地看着果子。
果子吓了一跳,正要说点什么,却突然觉得这个人有点诡异。在仔细一看,此人虽然是
面对自己,但其实脸上戴着一张黄金面具。再看这个人的身上,穿得是五彩缤纷的袍子
,后脑上的带子中别着几根长长的鹰羽,倒像是个巫师的打扮。只是这人就这么一动不
动的坐着,让果子觉得很不自在。
两个人足足对视了有十分种的样子,果子这才突然发现,对面这人其实是睡着了!再仔
细听听,都能听到细小均匀的呼吸声。果子差点气乐了,心想这可真是自己吓自己。此
时她已经发现,那人的身边放着两样东西,一样是一把鹅毛大扇,另一样是一只盘子,
里面好像放了一些食物。果子只是一扫之间,已经震惊地发现,那些食物居然是炸薯片!
这下果子可有些凌乱了:要知道,土豆这种东西,那是哥伦布发现美洲大陆后才引到明
朝的,五代这个时候哪里来的炸薯片?
恰在此时,只见对面那人微微一动,紧接着伸了个懒腰,口中吟道:“大梦谁先觉,平
生我自知。草堂春睡足,窗外日迟迟。”看来是醒了。
(待续)
avatar
d*n
2
比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
{ {0, 2, 3},
{1, 3, 6, 6},
{2, 5}
}
请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
到那个数据结构里面去)?
我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };
avatar
p*o
3
想要方便就去用java把,不想要效率没必要折腾C++。
结构不变的图就是稀疏矩阵,一般都有文本输入格式,比如Matrix Market Exchange
Format,程序里面表示的方法也有很多,比如Compressed Row Storage,处理上亿
的点都没啥问题。

所以可以有

【在 d*******n 的大作中提到】
: 比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
: { {0, 2, 3},
: {1, 3, 6, 6},
: {2, 5}
: }
: 请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
: 到那个数据结构里面去)?
: 我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };

avatar
d*p
4
boost tuple is just out there :-)

所以可以有

【在 d*******n 的大作中提到】
: 比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
: { {0, 2, 3},
: {1, 3, 6, 6},
: {2, 5}
: }
: 请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
: 到那个数据结构里面去)?
: 我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };

avatar
d*n
5
The thing is, most of the time, the developer cannot decide what language to
use......
You probably got my question wrong. I am not trying to discuss an high-level theoretical data-structure issue. I am only asking a handy trick to get some practical step done.
What I am really looking for is, in C++, how do you conveniently put such an
array of arrays in some datastructure, say, using a couple of rows of codes?
For example, if it's just an array of int, you can do the following as a
handy way t

【在 p***o 的大作中提到】
: 想要方便就去用java把,不想要效率没必要折腾C++。
: 结构不变的图就是稀疏矩阵,一般都有文本输入格式,比如Matrix Market Exchange
: Format,程序里面表示的方法也有很多,比如Compressed Row Storage,处理上亿
: 的点都没啥问题。
:
: 所以可以有

avatar
d*n
6
For example, one thing I could do is the following, putting the integers in
a vector>.
avatar
d*n
7
I just thought of a dirty trick that's only applicable to the case of single-
digit integers. That is, using char**. What I don't like about this solution
is that it uses 2-layer loops (a "for" and a "while"). Is there a way to do
this with only one loop?
#include
#include
using namespace std;
int main() {
char *s[]={"123","254","23","8640","1253","","253","80","2054","253"};
char** ss = s;
vector< vector > v(10);
for(int i = 0; i < 10; ++i){
char

【在 d*******n 的大作中提到】
: 比如有下面这个数组由3个小数组组成,每个小数组的size是不一样的?
: { {0, 2, 3},
: {1, 3, 6, 6},
: {2, 5}
: }
: 请问怎么样最方便的表示这个数组的数组并且可以方便的初始化(就是说把这些数都写
: 到那个数据结构里面去)?
: 我想要的是类似Java里面的“数组的数组”的东西,java里面的数组是一个class,所以可以有
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };

avatar
d*p
8
boost::tuple< boost::tuple, boost::tuple >
...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
...
x.get<0>().get<1>() = 2;
...
Whenever you resort to tricks, things are really going wrong. C++ has
everything you want for normal tasks.
avatar
p*o
9
WoW!

【在 d****p 的大作中提到】
: boost::tuple< boost::tuple, boost::tuple >
: ...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
: ...
: x.get<0>().get<1>() = 2;
: ...
: Whenever you resort to tricks, things are really going wrong. C++ has
: everything you want for normal tasks.

avatar
p*o
10
Why don't you put them into a file? Then you don't need a trick.
People have been working on these for tens of years. Google what
I said in the previous post and don't reinvent wheels.

single-
solution
do

【在 d*******n 的大作中提到】
: I just thought of a dirty trick that's only applicable to the case of single-
: digit integers. That is, using char**. What I don't like about this solution
: is that it uses 2-layer loops (a "for" and a "while"). Is there a way to do
: this with only one loop?
: #include
: #include
: using namespace std;
: int main() {
: char *s[]={"123","254","23","8640","1253","","253","80","2054","253"};
: char** ss = s;

avatar
p*o
11
OK, let me tell you what to do.
int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };
int crs_value[] = {0,2,3,1,3,6,6,2,5};
int crs_index[] = {0,3,7,9};
now twoDimArray[i][j]=crs_value[crs_index[i]+j];

to
level theoretical data-structure issue. I am only asking a handy trick to
get some practical step done.
an
codes?

【在 d*******n 的大作中提到】
: The thing is, most of the time, the developer cannot decide what language to
: use......
: You probably got my question wrong. I am not trying to discuss an high-level theoretical data-structure issue. I am only asking a handy trick to get some practical step done.
: What I am really looking for is, in C++, how do you conveniently put such an
: array of arrays in some datastructure, say, using a couple of rows of codes?
: For example, if it's just an array of int, you can do the following as a
: handy way t

avatar
t*t
12
not really. initializing complex class structure with constants are missing
from c++03. c++0x is extending it with std::initializer_list<>.

【在 d****p 的大作中提到】
: boost::tuple< boost::tuple, boost::tuple >
: ...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
: ...
: x.get<0>().get<1>() = 2;
: ...
: Whenever you resort to tricks, things are really going wrong. C++ has
: everything you want for normal tasks.

avatar
d*n
13
Thanks a lot!
I did learn a lot (how to use tuple) starting from reading this post . But
this solution might not work as well as it looks.
The problem is, you need to hard-code your tuple structure, don't you?

【在 d****p 的大作中提到】
: boost::tuple< boost::tuple, boost::tuple >
: ...x( boost::tuple<1, 2>, boost::tuple<3, 4, 5> >
: ...
: x.get<0>().get<1>() = 2;
: ...
: Whenever you resort to tricks, things are really going wrong. C++ has
: everything you want for normal tasks.

avatar
c*u
14
struct A
{
A(int num1, int num2, int num3 ...)
{
para.line1[0]=num1;
...
};
struct
{
int line1[2];
int line2[3];
}para;

} ;
avatar
d*p
15
Initialize dynamic array in aggregate form? You will have it in c++0x.
std::vector myVec{1, 2, 3};
...

【在 d*******n 的大作中提到】
: Thanks a lot!
: I did learn a lot (how to use tuple) starting from reading this post . But
: this solution might not work as well as it looks.
: The problem is, you need to hard-code your tuple structure, don't you?

avatar
d*n
16
Thanks!

【在 p***o 的大作中提到】
: OK, let me tell you what to do.
: int[][] twoDimArray = { {0,2,3}, {1,3,6,6}, {2, 5} };
: int crs_value[] = {0,2,3,1,3,6,6,2,5};
: int crs_index[] = {0,3,7,9};
: now twoDimArray[i][j]=crs_value[crs_index[i]+j];
:
: to
: level theoretical data-structure issue. I am only asking a handy trick to
: get some practical step done.
: an

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。