0

In C++ creating and manipulating a mutable stack of characters is rather simple. I use a built-in standard data type (std::string), use a push and pop fuctions that comes with it and can directly print the results. None of this requires creating additional classes or functions.

#include <iostream>

int main()
{
    std::string path {};
    path.push_back('L');
    path.push_back('R');
    path.pop_back();
    std::cout << path;
}

Produces:

L 

What is Kotlin for C++ push_back() and pop_back() as a stack of characters?

The question is not how can I implement these as member functions in Kotlin.

C++ Shell code

CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
  • Does this answer your question? [How can I use stack in Kotlin?](https://stackoverflow.com/questions/46900048/how-can-i-use-stack-in-kotlin) – mightyWOZ Feb 15 '20 at 16:09
  • You can use [Deque](https://docs.oracle.com/javase/10/docs/api/java/util/Deque.html) from java collections. – mightyWOZ Feb 15 '20 at 16:16

2 Answers2

2

Strings are immutable, so you won't find an exact equivalent. While StringBuilder is generally considered to be a decent string buffer type, deleteCharAt copies the entire backing array.

You can use setLength for this purpose. Note that, as with std::string, this does not do anything more than modify the internal length of the string buffer:

val string = StringBuilder("Hello")
string.append(", world!")
string.setLength(string.length - 1)

Alternatively, take the more functional approach and work with immutable values:

var string = "Hello"
string += ", world!"
string = string.dropLast(1)

As already stated by other answerers/commenters, if what you really want is a stack or deque, see Deque and ArrayDeque (seeing as you did not mention std::stack or std::deque in your question, I do not believe this to be the case; not to mention that a stack of characters isn't inherently very useful.)


Taking OP's comment into account, this should be an idiomatic solution:

enum class Move {
    Left, Right, Forward
}

val moves = ArrayDeque<Move>()

// Push a move:
moves.push(Move.Left)

// Pop a move:
val lastMove = moves.pop()

See also Why should I use Deque over Stack? for an explanation of Java's unfortunate history.

Salem
  • 13,516
  • 4
  • 51
  • 70
  • A maze like a mouse traverses with L(eft) R(ight) F(orward) steps can use a simple sequence of characters which when used as a stack can track the path. – CW Holeman II Feb 15 '20 at 17:23
  • 1
    @CWHolemanII ah. In that case I would use a deque of enums; though I would probably have done this in C++ as well. I'll update my answer... – Salem Feb 15 '20 at 17:29
0

I'm not really sure if this is what you mean, but Kotlin has a dropLast(param: number of characters to drop) function which returns a string without the number of characters you did want to remove. Link: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/drop-last.html

If you are looking for a stack data structure, you might take a look at the Stack and Deque data structures. https://chercher.tech/kotlin/deque-kotlin, https://chercher.tech/kotlin/stack-kotlin

gummy
  • 123
  • 1
  • 6