gpt4 book ai didi

STL - 如何在 Ada 中进行 std::rotate?

转载 作者:行者123 更新时间:2023-12-05 09:27:05 24 4
gpt4 key购买 nike

相当于std::rotate存在于 Ada 标准库中吗?如果不是,Ada 社区是否对此类操作的某些非标准算法库达成共识?我找到了 Ada Standard Generic Library但它是 1996 年的概念证明,不包括数组的旋转(但它适用于树)。

我在上面链接的 cppreference 页面上删减了演示:

#include <algorithm>
#include <iostream>
#include <vector>

auto print = [](auto const& remark, auto const& v) {
std::cout << remark;
for (int n : v)
std::cout << n << ' ';
std::cout << '\n';
};

int main()
{
std::vector<int> v{2, 4, 2, 0};

print("before rotate:\t\t", v);

// simple rotation to the left
std::rotate(v.begin(), v.begin() + 1, v.end());

print("simple rotate left:\t", v);

// simple rotation to the right
std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());

print("simple rotate right:\t", v);
}

如果我有一个替代 std::rotate 调用的替代品,等效的 Ada 将是这样的:

with Ada.Text_IO;
with Ada.Characters;
with Ada.Characters.Latin_1;

procedure Main is
Tab : Character renames Ada.Characters.Latin_1.HT;

type Number_List_Type is array (1 .. 4) of Integer;

procedure Print (Remark : String; Numbers : Number_List_Type) is
use Ada.Text_IO;
Endline : Character renames Ada.Characters.Latin_1.LF;
begin
Put (Remark);
for N of Numbers loop
Put (N'Image & ' ');
end loop;
Put (Endline);
end;

V : Number_List_Type := (2, 4, 2, 0);
begin
print("before rotate:" & Tab & Tab, v);

-- simple rotation to the left
-- std::rotate(v.begin(), v.begin() + 1, v.end());

print("simple rotate left:" & Tab, v);

-- simple rotation to the right
-- std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());

print("simple rotate right:" & Tab, v);
end Main;

预期输出:

before rotate:          2 4 2 0
simple rotate left: 4 2 0 2
simple rotate right: 2 4 2 0

最佳答案

在 Ada 中,您可以使用切片、串联和赋值来旋转数组元素:

v := v(2..4) & V(1..1);

如果你需要一个通用的解决方案,你可以这样做:

generic
type Element_Type is private;
type Index_Type is (<>);
type Element_Array is array(Index_Type range <>) of Element_Type;
function Rotate_1(Source: Element_Array; N_First : Index_Type) return Element_Array;

function Rotate_1(Source: Element_Array; N_First : Index_Type) return Element_Array is
Source_Last : constant Index_Type := Source'Last;
Source_First : constant Index_Type := Source'First;
begin

if N_First < Source_First then
raise Constraint_Error;
end if;

if N_First = Source_First then
return Source;
end if;

return Result : Element_Array(Source'Range)
:= Source(N_First .. Source_Last)
& Source(Source_First.. Index_Type'Pred(N_First));
end Rotate_1;

generic
type Element_Type is private;
type Index_Type is (<>);
type Element_Array is array(Index_Type range <>) of Element_Type;
procedure Rotate(Source: in out Element_Array; N_First : Index_Type);

procedure Rotate(Source: in out Element_Array; N_First : Index_Type) is
function r is new Rotate_1(Element_Type,Index_Type,Element_Array);
begin
Source := R(Source,N_First);
end Rotate;

请注意,我的通用示例需要一个不受约束的数组类型,因此您可以将数组类型声明更改为:

type Number_List_Type is array (Positive range <>) of Integer;

关于STL - 如何在 Ada 中进行 std::rotate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72910430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com