tgoop.com/cppproglib/6126
Create:
Last Update:
Last Update:
🦀 Транспиляция кода: от одного языка к другому
Представьте ситуацию: вы нашли идеальное решение на GitHub, но оно написано не на том языке. Или ваш руководитель прочитал статью о новом фреймворке, и теперь нужно переписывать всю кодовую базу. Знакомо?
Хорошая новость: современные LLM-модели отлично справляются с транспиляцией кода между языками программирования.
Rewrite the following code in C++:
[Вставь свой код сюда]
# Исходный Python код
def count_words(text):
"""Count word frequency in text"""
words = text.lower().split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
# Пример использования
text = "hello world hello python world"
print(count_words(text))
# Output: {'hello': 2, 'world': 2, 'python': 1}
Результат:
#include <iostream>
#include <string>
#include <map>
#include <sstream>
#include <algorithm>
std::map<std::string, int> count_words(const std::string& text) {
std::map<std::string, int> freq;
std::istringstream stream(text);
std::string word;
while (stream >> word) {
// Convert to lowercase
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
freq[word]++;
}
return freq;
}
int main() {
std::string text = "hello world hello python world";
auto result = count_words(text);
for (const auto& [word, count] : result) {
std::cout << word << ": " << count << std::endl;
}
return 0;
}
Библиотека C/C++ разработчика
#буст