• HOME
  • ABOUT US
  • SERVICES
  • PORTFOLIO
  • BLOG
Connection Quest Connection Quest
  • HOME
  • ABOUT US
  • SERVICES
  • PORTFOLIO
  • BLOG
Connection Quest

Sequencing

Home / Clean C++ / Sequencing

Sequencing

By Marko Jovic inClean C++

As software engineers, it’s fairly common for us to arrange things in sequences. We store and organize related objects by putting them in various collections. However, because there is a plethora of containers to choose from, it is important to select the one most fitting to the context. In this post, therefore, we’ll try to make a connection between sequence containers and appropriate use cases.

To avoid issues common with fixed arrays, such as their decay to pointers and lost length information, you can use the std::array container. Besides dealing with aforementioned issues, it is efficient, doesn’t use more memory than built-in fixed arrays and has a number of handy utility functions.

void getSize(const std::array<int, 3> &staticArray) {
	std::cout << "The array has a length of: " << staticArray.size();
}

int main() {
	std::array<int, 3> staticArray { 4, 6, 2 }; 
    getSize(staticArray); 
	return 0;
}

Of course, if you need dynamic array functionality, then it’s not enough. True, you can always allocate a built-in array of a required length, but you still have to be very careful when deallocating or resizing that array.

But fear not, std::vector to the rescue! Similar to std::array, std::vector is a container that makes working with dynamic arrays safer and easier. It handles its own memory so you don’t need to, and can be easily resized during its lifetime. After it goes out of scope, it automatically cleans up after itself, preventing memory leaks.

std::vector<int> dynamicArray {4, 6, 2}; // vector size is now 3
dynamicArray.resize(10); // vector size is now 10

You can access elements in a std::array or std::vector in two ways: by using the familiar [] operator or the at() function. The subscript operator does not do any bounds–checking, so you have to be extremely cautious not to access a piece of memory outside the container. The at() function, on the other hand, does index checking, so it will throw you a std::out_of_range exception if you mess up. Conclusively, you should prefer it over the built in [] operator.

std::vector<int> dynamicArray {4, 6, 2};
dynamicArray.at(1) = 5; // valid
dynamicArray.at(3) = 10; //invalid, throws an exception

Whichever container you’re using, if it implements iterators, prefer using a for-each loop to iterate over it. If you don’t need to access each index, that is. For-each loops provide an easier way to go over a collection in a forwards sequential order. Furthermore, to avoid making copies, the element declaration should ideally be a reference.

std::vector<std::string> dynamicArray { “Hi”, “Hello”, “Howdy”};
for (const auto &greeting : dynamicArray)
	std::cout << greeting << “ traveler!”;

There are a lot situations where you might want to transfer sequences between functions. When passing these containers as an argument to a function, be sure to avoid needless element copying and use references (preferably const).

void processSequence(const std::vector<int> &sequence) {
	...
}

On the other hand, when returning a sequence, use a simple return-by-value concept.

std::vector<int> getSequence() {
	std::vector<int> sequence {1, 2, 3};
	...
 
	return sequence;
}

In the example above, the sequence elements are not actually copied but moved to its new container. As of C++11, standard library containers have defined move semantics, which makes execution of these and similar use-cases extremely efficient.

Needless to say, std::array and std::vector are not the only containers defined in the C++ standard library. Be it sequence, associative or unordered associative, rest assured that there is a container for everything you need to do. But that is a topic for another blog post, until then…

Stay tuned and happy coding.

AdviceArraysC++Clean CodeCollectionsConventionsSequencesSoftwareSoftware DevelopmentVectors
15 Posts
Marko Jovic
  • Qt Environment for Android
    Previous PostQt Environment for Android
  • Next PostConcurrency control
    Qt Environment for Android

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

© 2025 Connection Quest j.d.o.o. All rights reserved.

Copy