What does iota of std::iota stand for?
C++ iota
is not an acronym or an initialism. It is the word “iota”, which is the English spelling of the name of the ninth letter of the Greek alphabet: ι. The lower-case version of the letter is customarily drawn like an English letter i, but without the dot. Depending on your font, the serifs may differ slightly.
The original SGI STL documentation gives this explanation:
The name
iota
is taken from the programming language APL.
Ken Iverson created APL (which stands for “A Programming Language”). In §1.7 “Special vectors” of his 1962 book A Programming Language, he introduced ⍳ (iota) on page 15 as the “interval vector”:
The interval vector ⍳j(n) is defined as the vector of integers beginning with j.
In his 1979 Turing Award lecture, “Notation as a Tool of Thought”, he gave this description of APL's ⍳ function:
For example, the integer function denoted by
⍳
produces a vector of the firstn
integers when applied to the argumentn
, …
So the likeliest answer is that Dr. Iverson wanted a symbol which would remind the user of the word “integer”, the word “interval”, and the use of the letter “i” as a typical integer variable, especially for array subscripting.
Sean Parent suggests that ‘[t]he “ι” symbol was likely chosen because of its use to represent an inclusion map in mathematics.’ He explains his reasoning here.
Sadly, Dr. Iverson died in 2004, several years before this question was even asked on Stack Overflow, so we cannot ask him for a more precise explanation.
By the way, Unicode has two code points for lower-case iota: one for writing Greek and the other for writing APL. The one for writing Greek is ι
, U+03B9, “GREEK SMALL LETTER IOTA”. The one for writing APL is ⍳
, U+2373, “APL FUNCTIONAL SYMBOL IOTA”.
In response to the demands of commenters, I shall further address the etymology of “iota” in this context.
Let's suppose there is a deeper meaning.
According to the Oxford English Dictionary, “iota” is “The name of the Greek letter Ι, ι, corresponding to the Roman I, i; the smallest letter of the Greek alphabet” (smallest physically, not alphabetically, I presume), and also means “The least, or a very small, particle or quantity”. The OED's earliest known usage of this meaning is from Clavis mystica by Daniel Featley in 1636:
Shall we lose, or sleightly pass by, any iota or tittle of the Booke of God?
Clavis mystica describes itself as “a key opening divers difficult and mysterious texts of Holy Scripture”, and this sentence is in particular referring to Matthew 5:18. The 1611 edition of the King James Version has this text for Matthew 5:18:
Transcription:
For verily I say vnto you, Till heauen and earth passe, one iote or one title, shall in no wise passe from the law, till all be fulfilled.
In the original Greek of Matthew 5:18, “iote” is “ἰῶτα”, and “title” (or more modernly, “tittle”) is “κεραία”. The word “κεραία” meant, roughly, “serif” or “apostrophe”. So this Bible verse is referring to the idea of the smallest details, and using “ἰῶτα” to refer to the letter iota in its role as the physically smallest letter of the Greek alphabet.
Featley, as a Bible scholar (he earned a Doctorate of Divinity), could undoubtedly read Greek. I guess he just transliterated the Greek name for the letter, “ιώτα”, into English “iota”. Why didn't he follow the King James translation's spelling, “iote“? I don't know.
Thus we may deduce that the STL function iota
, and its APL antecedent ⍳
, are named, by way of the Bible, after the physically smallest letter of the Greek alphabet “ι”, because these functions produce integers separated by the smallest amount by which integers may be separated.
According to Wikipedia, The Greek letter iota came from the Phoenician letter yōdh.
This is as far afield of programming as I currently wish to go for this question.
Why is it Called iota?
Quoting this non-authoritative, but nonetheless correct, wiki:
The function is named after the integer function
⍳
from the programming language APL.
In APL, the ⍳
function (represented with the ninth letter of the Greek alphabet, iota) is used to create a zero-based array of consecutive, ascending integers of a specified length.
std::iota is very limited
But how would one do:
x = range(0,20,2)
Alternatively to std::generate()
(see other answer), you can provide your own unary function to std::iota()
, it just have to be called operator++()
:
#include <iostream>
#include <functional>
#include <numeric>
#include <vector>
template<class T>
struct IotaWrapper
{
typedef T type;
typedef std::function<type(const type&)> IncrFunction;
type value;
IncrFunction incrFunction;
IotaWrapper() = delete;
IotaWrapper(const type& n, const IncrFunction& incrFunction) : value(n), incrFunction(incrFunction) {};
operator type() { return value; }
IotaWrapper& operator++() { value = incrFunction(value); return *this; }
};
int main()
{
IotaWrapper<int> n(0, [](const int& n){ return n+2; });
std::vector<int> v(10);
std::iota(v.begin(), v.end(), n);
for (auto i : v)
std::cout << i << ' ';
std::cout << std::endl;
}
Output: 0 2 4 6 8 10 12 14 16 18
Demo
Here is an idea of how one could implement Range()
:
struct Range
{
template<class Value, class Incr>
std::vector<Value> operator()(const Value& first, const Value& last, const Incr& increment)
{
IotaWrapper<Value> iota(first, [=](const int& n){ return n+increment; });
std::vector<Value> result((last - first) / increment);
std::iota(result.begin(), result.end(), iota);
return result;
}
};
Demo
How to use std::iota with std::pair?
Please, note that std::iota() has separate types for the iterator and the value to increment/assign:
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
This can be utilized to use a type for value which is different from (though assignable to) the element type of vector.
In my case, I simply derived a type from the std::pair
(to be used in the std::vector
) and added the increment operator to that derived type:
#include <numeric>
#include <iostream>
#include <vector>
template <typename T1, typename T2>
struct PairIncT: std::pair<T1, T2> {
PairIncT(T1 first, T2 second): std::pair<T1, T2>(first, second) { }
PairIncT& operator++() { ++this->first; ++this->second; return *this; }
};
int main()
{
// a vector of pairs
using VectorIU = std::vector<std::pair<int, unsigned>>;
VectorIU v(5);
// make corresponding inc. type for vector element
using PairIU = PairIncT<
VectorIU::value_type::first_type,
VectorIU::value_type::second_type
>;
// fill vector
std::iota(v.begin(), v.end(), PairIU( 1, 2 ));
// show vector
for (const VectorIU::value_type &elem : v) {
std::cout << "{ " << elem.first << ", " << elem.second << " }\n";
}
}
Output:
{ 1, 2 }
{ 2, 3 }
{ 3, 4 }
{ 4, 5 }
{ 5, 6 }
Live Demo on coliru
iota increment vector of a class c++
I am not sure I understand your question. Does the following code match what you want?
#include <algorithm>
#include <iostream>
#include <vector>
class Object {
public:
Object(int value = 0)
: m_value(value) { }
Object& operator++() {
m_value++;
return *this;
}
int value() const {
return m_value;
}
private:
int m_value;
};
int main() {
std::vector<Object> os(10);
std::iota(os.begin(), os.end(), 0);
for(const auto & o : os) {
std::cout << o.value() << std::endl;
}
}
Compiled with gcc 4.8 on OS X 10.7.4 I get:
$ g++ iota-custom.cpp -std=c++11
$ ./a.out
0
1
2
3
4
5
6
7
8
9
Iterator or std::iota with regular C++ Array?
C++11 added std::begin
and std::end
. Since then there is no difference:
std::list<int> l(10);
std::iota(std::begin(l),std::end(l), -4);
int a[10];
std::iota(std::begin(a),std::end(a), -4);
Related Topics
Enumdisplaydevices VS Wmi Win32_Desktopmonitor, How to Detect Active Monitors
Calculate System Time Using Rdtsc
Recursion in C++ Factorial Program
Do Function Pointers Need an Ampersand
Narrowing Conversion from Unsigned to Double
How to Get a Non-Const C String Back from a C++ String
How to Tell Reliably If a Boost Thread Has Exited Its Run Method
How Copy from One Stringstream Object to Another in C++
How to Handle a Transitive Dependency Conflict Using Git Submodules and Cmake
Why Should I Avoid MACros in C++
C++ Convert from Lpctstr to Const Char *
Why Does Sizeof(Int) Vary Across Different Operating Systems
Stoi and Std::To_String on Mingw 4.7.1
Image Retrieval System by Colour from the Web Using C++ with Openframeworks