c_cpp 茴字的四种写法(C ++)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 茴字的四种写法(C ++)相关的知识,希望对你有一定的参考价值。
//
// returnArray.cpp
// return array as reference.
//
// Created by pezy on 14/10/30.
// Copyright (c) 2014 pezy. All rights reserved.
//
#include <iostream>
#include <string>
using std::string; using std::cout; using std::endl;
using ArrT = string[10];
ArrT& func1(ArrT& arr)
{
arr[0] = "pezy@1";
arr[1] = "mooophy@1";
arr[2] = "quee@1";
return arr;
}
auto func2(ArrT& arr) -> string(&)[10]
{
arr[0] = "pezy@2";
arr[1] = "mooophy@2";
arr[2] = "quee@2";
return arr;
}
string arrS[10];
decltype(arrS)& func3(ArrT& arr)
{
arr[0] = "pezy@3";
arr[1] = "mooophy@3";
arr[2] = "quee@3";
return arr;
}
string (&func(string (&arrStr)[10]))[10]
{
arrStr[0] = "pezy";
arrStr[1] = "mooophy";
arrStr[2] = "quee";
return arrStr;
}
int main(int argc, const char * argv[]) {
string arrStr[10];
for (const auto& str : func(arrStr))
if (!str.empty())
cout << str << endl;
return 0;
}
#include <iostream>
using std::cout; using std::endl; using std::begin; using std::end;
int main()
{
constexpr size_t rowCnt = 3, colCnt = 4;
int ia[rowCnt][colCnt];
// subscirpting
for (size_t i = 0; i != rowCnt; ++i)
for (size_t j = 0; j != colCnt; ++j)
ia[i][j] = i*colCnt + j;
// range for
size_t cnt = 0;
for (auto &row : ia)
for (auto &col : row)
col = cnt++;
// output (use range for)
for (const auto& row : ia)
for (auto col : row)
cout << col << " ";
cout << endl;
// output (use pointers)
for (auto p = begin(ia); p != end(ia); ++p)
for (auto q = begin(*p); q != end(*p); ++q)
cout << *q << " ";
cout << endl;
// output (use type aliases)
using int_array = int[4];
for (int_array *p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}
以上是关于c_cpp 茴字的四种写法(C ++)的主要内容,如果未能解决你的问题,请参考以下文章
专访朱雷:昔日的游戏少年,如今的Python工匠
Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理
.Net 7 托管Main入口的四种写法(茴香豆?)
Android笔记---点击事件的四种写法
交换函数swap的四种写法
java保留小数后两位的四种写法